package Server.Poker.ZJH;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Vector;

import org.apache.commons.lang.StringUtils;
import org.json.JSONObject;

import Server.Agent.GameAgent;
import Server.Agent.GameConfig;
import Server.Agent.GameDB;
import Server.Model.Base.IRoom;
import Server.Model.Base.IServerUserItem;
import Server.Model.Base.SHMUser;
import Server.Model.Hall.QpGameMgr;
import Server.Model.Hall.dthall;
import activity.ActivityMgr;
import activity.UserActionActivity;
import admin.AdminRedis;
import club.ClubMgr;
import common.routine.CommonDef;
import common.routine.PublishDef;
import rank.RankMgr;
import redis.protocal.DbInfoMgr;
import redis.protocal.DwcRedisRecord;
import redis.protocal.UserRedisInfo;
import robot.GoldMgr;
import robot.Robot;
import robot.RobotMgr;
import Server.Model.Base.SecShmUser;
import Server.Model.Base.UserGoldChange;
import Server.Model.Base.UserRmbChange;

class ZjhTable extends IRoom {
	//Log log = LogFactory.getLog(this.getClass());
	public Integer m_dwTableID;

	public Long m_lStartTime = 0L;
	public int m_lCellScore = 0; // 房间倍数
	// 游戏变量

	public int m_wCurrentUser; // 当前用户
	public int m_bSendCardCount; // 发牌数目
	public int m_bTableCardCount[] = new int[5]; // 扑克数目
	public int m_bTableCardArray[][] = new int[5][3]; // 桌面扑克 //been 5
	public int m_cbCardStatus[] = new int[5]; // 卡牌状态
	public boolean start; // 游戏开始或下一轮状态 //add
	public int Winner; // 赢家椅子号

	public int m_lTurnBasicGold; // 每一次最少下注
	public int m_lTableGoldTotal;
	public int m_lTableGold[] = new int[5]; // 下注金币，每次下注金币
	public int m_cntNotReady[] = new int[5]; // 没有ready的局数

	public int m_cntTableChip[] = new int[6]; // 桌子上每种筹码有多少枚

	public boolean m_bKanPai[] = new boolean[5]; // 看牌状态
	public int m_wRobotSeconds[] = new int[5]; // 等待时长
	public int m_wRobotYongQi[] = new int[5]; // 勇气
	public int m_wBiPaiDelay;
	public int m_wFaPaiDelay;// 发牌延迟

	public Long m_dismissTimer;

	public int m_byAgreeDismiss[] = new int[5];
	// 状态变量

	public int m_chipRound = 0;//机器人使用
	
	public boolean m_bTableStarted; // 开始标志
	public boolean m_bGameStarted; // 开始标志
	public boolean m_jiesuan;
	public int m_byRound;
	public int m_byMaxRound;
	public int maxPeople = 5;
	public int m_byCostGem;
	public Long m_lRoundLimit = 30L; // 最大下注
	public int m_diFen = 1; // 最大下注
	public int m_menQuan = 0; // 闷几圈
	public int m_staticmenQuan = 0; // 闷几圈
	public String m_payWay = "";
	public String m_strParam = "";
	public String m_strRoomDesc = "";	
	
	public int m_byZhuang;// 当前庄位置
	public int m_vecWinScore[] = new int[5];
	public int m_vecLoseScore[] = new int[5];
	public int m_vecCardType[] = new int[5];
	public int m_vecRoundScore[] = new int[5];
	public int m_vecResultScore[] = new int[5];
	public double m_doubleVecResultScore[] = new double[5];
	public double m_doubleKouWinResultScore[] = new double[5];
	public int m_kouWinSumJu = 0;
	public int m_kouWinCurrJu = 0;
	public int m_cntGenZhu[] = new int[5];
	// 组件变量
	int m_locked = 0;
	public int m_WhoTryDismiss = 0;
	String m_curStage;

	public IServerUserItem[] m_pIUserItem = new IServerUserItem[5];// 用户

	public int needDealTime;
	public GameLogic m_GameLogic = new GameLogic();
	public int m_Creator;// 房主
	public int m_cntCtrl = 0;
	{
		m_payWay = "FZPAY";
		for (int i = 0; i < 5; i++) {
			m_pIUserItem[i] = new IServerUserItem();
			m_pIUserItem[i].Init();
		}
		m_cntCtrl = 0;
		Init(INIT_TOTAL);
	}
	@Override
	public int GetID() {
		// TODO Auto-generated method stub
		return m_dwTableID;
	}

	@Override
	public String GetType() {
		// TODO Auto-generated method stub
		return "ZJH";
	}
	@Override
	public int GetCreator() {
		// TODO Auto-generated method stub
		return m_Creator;
	}
	@Override
	public void OnClientClose(int uid) {
		// TODO Auto-generated method stub
		OnUserStandUp(uid,false);
	}

	@Override
	public int FindSeat(int _Uid) {
		// TODO Auto-generated method stub
		for (int bySeatID = 0; bySeatID < 5; bySeatID++) {
			if (m_pIUserItem[bySeatID].dwUserID == _Uid) {
				return bySeatID;
			}
		}
		return Constants.INVALID_CHAIR;
	}
	public void SendLocationData(SHMUser user) {
		JSONObject data = new JSONObject();
		data.put("loc", user.getGpsLocInfo());
		data.put("uid", user.getUid());
		data.put("command", "GPS_LOC");
		SendTableData(Constants.INVALID_CHAIR, data.toString());

		for (short i = 0; i < m_pIUserItem.length; i++) {
			if (m_pIUserItem[i].dwUserID > 0) {
				if (m_pIUserItem[i].isAndroidUser == 0) {
					int uid = m_pIUserItem[i].dwUserID;
					SHMUser other = dthall.getInstance().getUserInfo(uid);
					if(other != null){
						data.put("uid", other.getUid());
						data.put("loc", other.getGpsLocInfo());
						dthall.getInstance().onSend(user.getUid(), data.toString());
					}
				}
			}
		}
	}

	public void SendGpsData(SHMUser user) {
		JSONObject data = new JSONObject();
		List<Double> gpsData = user.getLocateInfo();
		data.put("lng", gpsData.get(0));
		data.put("lat", gpsData.get(1));
		data.put("command", "GPS_DATA");
		data.put("uid", user.getUid());
		SendTableData(Constants.INVALID_CHAIR, data.toString());

		for (short i = 0; i < m_pIUserItem.length; i++) {
			if (m_pIUserItem[i].dwUserID > 0) {
				if (m_pIUserItem[i].isAndroidUser == 0) {
					int uid = m_pIUserItem[i].dwUserID;
					SHMUser other = dthall.getInstance().getUserInfo(uid);
					if (other != null) {
						gpsData = other.getLocateInfo();
						data.put("uid", other.getUid());
						data.put("lng", gpsData.get(0));
						data.put("lat", gpsData.get(1));
						dthall.getInstance().onSend(user.getUid(), data.toString());
					}
				}
			}
		}
	}
	@Override
	public void OnUserSit(int _Uid) {
		// TODO Auto-generated method stub
		int bySeatID = FindSeat(_Uid);
		if (bySeatID < 5) {
			m_pIUserItem[bySeatID].cbOnline = US_NULL;
			m_dismissTimer = (long) 0;
		}
		SHMUser user = dthall.getInstance().getUserInfo(_Uid);
		if (user != null) {
			SendGpsData(user);
			SendLocationData(user);
		}
		if (user != null) {
			for (int i = 0; i < 5; i++) {
				if (m_pIUserItem[i].dwUserID == _Uid) {
					m_wRobotSeconds[i] = 0;
					m_pIUserItem[i].dwUserID = _Uid;

					m_pIUserItem[i].wChairID = (int) i;
					m_pIUserItem[i].dwFaceID = 0;
					m_pIUserItem[i].cbGender = user.getSex();
					m_pIUserItem[i].szNickName = user.getNickName(null);
					m_pIUserItem[i].szIP = user.getIP(null);
					m_pIUserItem[i].lUserCoin = user.getCoin(0);
					if(this.isDwcType()){
						m_pIUserItem[i].lUserRmb = user.getRmb1v1Value();
					}
					if(user.isAndroidPlayer()){
						m_pIUserItem[i].isAndroidUser = 2;
					}

					SUBCMD_S_Sit cmd = new SUBCMD_S_Sit();
					//

					cmd.m_lUid = _Uid;
					cmd.m_wChairID = m_pIUserItem[i].wChairID;
					cmd.m_dwTableID = m_dwTableID;
					cmd.m_lUserScore = m_pIUserItem[i].lUserScore;
					cmd.m_cbGender =  m_pIUserItem[i].cbGender;
					cmd.m_lBetCoin = m_lTableGold[i];
					cmd.szNickName = m_pIUserItem[i].szNickName;
					cmd.szIP = m_pIUserItem[i].szIP;
					cmd.m_cbUserStatus = m_pIUserItem[i].cbUserStatus;
					cmd.m_lUserCoin = user.getCoin(0);
					if(this.isDwcType())
						cmd.m_lUserRmb = user.getRmb1v1Value();
					SendTableData(Constants.INVALID_CHAIR, cmd.getJsonData());

					if (m_bGameStarted) {
						SendGameScene(_Uid, Constants.GS_PLAYING);
					} else {
						SendGameScene(_Uid, Constants.GS_FREE);
					}

					SendAvatar(m_pIUserItem[i].dwUserID);

					return;
				}
			}
			for (int i = 0; i < 5; i++) {
				if (m_pIUserItem[i].dwUserID == 0) {
					m_wRobotSeconds[i] = 0;
					m_pIUserItem[i].dwUserID = _Uid;
					m_pIUserItem[i].lUserScore = 1000;
					m_pIUserItem[i].wChairID = (int) i;
					m_pIUserItem[i].dwFaceID = 0;
					m_pIUserItem[i].cbGender = user.getSex();
					m_pIUserItem[i].szNickName = user.getNickName(null);
					m_pIUserItem[i].szIP = user.getIP(null);
					m_pIUserItem[i].lUserCoin = user.getCoin(0);
					if(this.isDwcType()){
						m_pIUserItem[i].lUserRmb = user.getRmb1v1Value();
					}
					if(user.isAndroidPlayer()){
						m_pIUserItem[i].isAndroidUser = 2;
					}

					SUBCMD_S_Sit cmd = new SUBCMD_S_Sit();
					//

					cmd.m_lUid = _Uid;
					cmd.m_wChairID = m_pIUserItem[i].wChairID;
					cmd.m_dwTableID = m_dwTableID;
					cmd.m_lUserScore = m_pIUserItem[i].lUserScore;
					cmd.m_cbGender =  m_pIUserItem[i].cbGender;
					cmd.m_lBetCoin = m_lTableGold[i];
					cmd.szNickName = user.getNickName(null);
					cmd.szIP = user.getIP(null);
					cmd.m_cbUserStatus = m_pIUserItem[i].cbUserStatus;
					cmd.m_lUserCoin = user.getCoin(0);
					if(this.isDwcType()){
						cmd.m_lUserRmb = user.getRmb1v1Value();
					}

					SendTableData(Constants.INVALID_CHAIR, cmd.getJsonData());

					if (m_bGameStarted) {
						SendGameScene(_Uid, Constants.GS_PLAYING);
					} else {
						SendGameScene(_Uid, Constants.GS_FREE);
					}

					return;
				}
			}
		}
	}

	@Override
	public void OnUserStandUp(int _Uid,boolean leaveroom) {
		int wChairID = FindSeat(_Uid);
		
		if(m_bTableStarted == true && this.isDwcType() && leaveroom == true){
			//真金情况可以退出
			SUBCMD_S_StandUp cmd = new SUBCMD_S_StandUp();
			cmd.m_lUid = _Uid;
			cmd.m_wUserChairID = wChairID;
			cmd.m_dwTableID = m_dwTableID;
			cmd.leaveRoom = leaveroom ?1:0;
			
			for(int i = 0; i < 5; i ++){
				if(m_pIUserItem[i].dwUserID > 0 && _Uid != m_pIUserItem[i].dwUserID)
				{
					SendData(m_pIUserItem[i].dwUserID, cmd.getJsonData());
				}
			}
			
			m_pIUserItem[wChairID].Init();
			if(hasAutoDianKong()){
				onUserQuitForDianKong(_Uid);
			}
			QpGameMgr.getInstance().OnRemoveUserRoom(_Uid);
			//检查是否全部是机器人
			int realCnt = 0;
			for(int i = 0; i < 5; i ++){
				if(m_pIUserItem[i].dwUserID > 0 && m_pIUserItem[i].isAndroidUser == 0)
				{
					realCnt++;
				}
			}
			if(realCnt == 0){
				OnEventGameEnd(0, GAME_END_DISMISS);
				return;
			}
			//是否全部准备好了
			{
				int cntReady = 0;
				for (int i = 0; i < 5; i++) {
					if (m_pIUserItem[i].cbUserStatus == US_READY) {
						cntReady++;
					}
				}
			
				int cntUser = 0;
				for (int i = 0; i < 5; i++) {
					if (m_pIUserItem[i].dwUserID > 0 && m_pIUserItem[i].cbUserStatus != Constants.US_LOOKON) {
						cntUser++;
					}
				}
			
				if (cntReady == cntUser && cntReady > 1) {
					OnEventGameStart();
				}
			}
			return;
		}
		if(m_bTableStarted == false && wChairID != 0 && leaveroom == true){
			SUBCMD_S_StandUp cmd = new SUBCMD_S_StandUp();
			cmd.m_lUid = _Uid;
			cmd.m_wUserChairID = wChairID;
			cmd.m_dwTableID = m_dwTableID;
			cmd.leaveRoom = leaveroom ?1:0;
			SendTableData(Constants.INVALID_CHAIR, cmd.getJsonData());
		}


		if (m_bTableStarted == false && leaveroom == true) {
			if (wChairID > 0 && wChairID < 5) {
				m_pIUserItem[wChairID].Init();
				if(hasAutoDianKong()){
					onUserQuitForDianKong(_Uid);
				}
				QpGameMgr.getInstance().OnRemoveUserRoom(_Uid);
			} else {
			}
		} else {
			if (wChairID < 5) {
				m_pIUserItem[wChairID].cbOnline = Constants.US_OFFLINE;
			}
		}
	}

	int random(int iMin, int iMax) {
		Random ra = new Random();
		return ra.nextInt(iMax - iMin) + iMin;
	}

	public static int GAME_END_NORMAL = 0;
	public static int GAME_END_PASS = 1;
	public static int GAME_END_ESCAPE = 2;
	public static int GAME_END_MAXROUND = 3;
	public static int GAME_END_DISMISS = 4;

	public static final int US_NULL = 0;
	public static final int US_READY = 1;
	public static final int US_PLAY = 2;

	public static final int INIT_NEWROUND = 2;
	public static final int INIT_TOTAL = 1;
	public static final int INIT_FIRST_ROUND = 0;

	public void Init(int cbReason) {
		// 游戏变量
		m_lCellScore = 1; // 房间倍数
		needDealTime = 15;
		m_bGameStarted = false;
		m_wBiPaiDelay = 0;
		m_wFaPaiDelay = 0;
		m_jiesuan = false;
		// Winner = 0; //add
		start = false; // add
		for (int i = 0; i < 5; i++) // add
		{
			m_bKanPai[i] = false;
			m_wRobotSeconds[i] = 0;

			m_cbCardStatus[i] = 0;

			m_cntNotReady[i] = 0;

			m_vecRoundScore[i] = 0;
			m_cntGenZhu[i] = 0;
		}

		m_bSendCardCount = 0;
		m_wCurrentUser = Constants.INVALID_CHAIR;

		// nWinner=1; //add

		// 金币信息
		m_lTurnBasicGold = m_lCellScore;
		m_lTableGoldTotal = 0;

		for (int i = 0; i < 5; i++) {
			m_bTableCardCount[0] = 0;
			m_bTableCardArray[i][0] = 0;
			m_bTableCardArray[i][1] = 0;
			m_bTableCardArray[i][2] = 0;
			m_lTableGold[i] = 0;
			m_bKanPai[i] = false;
			m_pIUserItem[i].Init();
		}

		m_cntTableChip[0] = 0;
		m_cntTableChip[1] = 0;
		m_cntTableChip[2] = 0;
		m_cntTableChip[3] = 0;
		m_cntTableChip[4] = 0;
		m_cntTableChip[5] = 0;

		switch (cbReason) {
		case INIT_TOTAL: {
			m_byZhuang = 0;
			m_dismissTimer = (long) 0;
			m_byAgreeDismiss[0] = 0;
			m_byAgreeDismiss[1] = 0;
			m_byAgreeDismiss[2] = 0;
			m_byAgreeDismiss[3] = 0;
			m_byAgreeDismiss[4] = 0;

			m_byRound = 0;
			m_byMaxRound = 0;
			
			for (int i = 0; i < 5; i++) {
				m_vecWinScore[i] = 0;
				m_vecLoseScore[i] = 0;
				m_vecResultScore[i] = 0;
				m_doubleVecResultScore[i] = 0;
			}
			m_lCellScore = 1; // 房间倍数
			m_diFen = 1;
		}
			break;
		case INIT_FIRST_ROUND: {

		}
			break;
		case INIT_NEWROUND: {

		}
			break;
		}

	}

	// 复位桌子
	public void RepositTable() {
		// 状态变量
		needDealTime = 20;
		m_bGameStarted = false;
		m_wBiPaiDelay = 0;
		m_wFaPaiDelay = 3;

		// Winner = 0; //add
		start = false; // add

		m_bSendCardCount = 0;

		for (int i = 0; i < 5; i++) {
			m_bTableCardCount[0] = 0;
			m_bTableCardArray[i][0] = 0;
			m_bTableCardArray[i][1] = 0;
			m_bTableCardArray[i][2] = 0;
			m_lTableGold[i] = 0;
			m_bKanPai[i] = false;
			// m_pIUserItem[i].Init();
		}

		// 金币变量
		m_lTurnBasicGold = m_lCellScore;
		m_lTableGoldTotal = 0;

		for (int i = 0; i < 5; i++) {
			m_pIUserItem[i].cbUserStatus = US_NULL;
			m_wRobotSeconds[i] = 0;
			m_cbCardStatus[i] = 0;
		}

		m_cntTableChip[0] = 0;
		m_cntTableChip[1] = 0;
		m_cntTableChip[2] = 0;
		m_cntTableChip[3] = 0;
		m_cntTableChip[4] = 0;
		m_cntTableChip[5] = 0;

		return;
	}

	public boolean OnEventGameStart() {
		m_bTableStarted = true;
		m_bGameStarted = true;

		if (m_byRound == 0) {
			m_byRound = 1;
		}
		m_kouWinSumJu = GoldMgr.getInstance().getKouWinJuShuInClub();
		if(this.isDwcType() && GoldMgr.getInstance().getNeedAutoDianKong() == 1){
			//需要自动点控则请求
			if(!hasAutoDianKong()){
				 requestDianKong(cloneServerUserPack(m_pIUserItem));
			}else{
				int uid = getDianKongUid();
				int seat = FindSeat(uid);
				if(seat == Constants.INVALID_CHAIR){
					clearDianKong();
				}
			}
		}
		FaPai();

		OnRoundInfoChange();
		return true;
	}

	public static int log_counter = 0;
	public void DoDismissRoomByOverTime(){
		OnEventGameEnd(0, GAME_END_DISMISS);
	}
	void OnEventGameEnd(int bySeatID,int cbReason){
		if(isZuJuType()){
			OnEventGameEndZuJu(bySeatID,cbReason);
		}else if(isGoldType()){
			onEventGameEndGold(bySeatID,cbReason);
		}else if(isDwcType()){
			onEventGameEndDwc(bySeatID,cbReason);
		}
	}
	public boolean onEventGameEndDwc(int wChairID, int cbReason) {
		m_bGameStarted = false;
		m_byRound++;
		OnRoundInfoChange();
		m_wFaPaiDelay = 3;

		int cntUser = 0;
		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].dwUserID > 0 && m_pIUserItem[i].isAndroidUser == 0 && Constants.US_LOOKON != m_pIUserItem[i].cbUserStatus) {
				cntUser++;
			}
		}
		
		switch (cbReason) {
		case 0:// GER_NORMAL: //常规结束
		case 0x10:// GER_NO_PLAYER: //没有玩家
		{
			double shuishourate = getShuiShou(this.GetType());
			double shui = this.m_doubleDiFen * shuishourate;
			if(shui < 0.01){
				shui = 0.0;
			}
			// 统计信息
			int wWinnerUser = DeduceWinner();

			Winner =  wWinnerUser;
			double []realshui = new double[5];
			for(int i = 0;i < 5;i++){
				realshui[i] = 0;
			}
			//扣税
			for(int i = 0;i < 5;i++){
				if (m_pIUserItem[i].dwUserID <= 0 || Constants.US_LOOKON == m_pIUserItem[i].cbUserStatus) {
					continue;
				}
				if(m_kouShuiMode.equals("no"))
					continue;
				if(m_kouShuiMode.equals("win") && i != Winner){
					continue;
				}else if(m_kouShuiMode.equals("lose") && i == Winner){
					continue;
				}else{
					//全部扣税
				}
				realshui[i] = shui;
				SecShmUser user = dthall.getInstance().getSecurityUserInfo(m_pIUserItem[i].dwUserID);
				//扣除税收先,最多扣完,不会扣成负数(系统不补贴)
				m_pIUserItem[i].lUserRmb -= shui;
				user.getRmb1v1(0 - shui);
				if(m_pIUserItem[i].isAndroidUser == 0){
					//正常用户
					UserRmbChange.handleRmbChangeKouShui(user, shui, UserRmbChange.reason6);
					this.addEatNormalScore(shui);
					handleRmbGiveBack(m_pIUserItem[i].dwUserID,shui);
					UserRedisInfo.addKouShuiFanXianForRent(m_pIUserItem[i].dwUserID,shui);
				}else if(m_pIUserItem[i].isAndroidUser == 2){
					//机器人
					this.addEatRobotScore(shui);
				}
			}
			
			double vecGameGold[] = new double[5];
			// 定义变量
			SUBCMD_S_JieSuan GameEnd = new SUBCMD_S_JieSuan();


			//乘上底注
			m_lTableGoldTotal *= 1;//不成底注，扎金花每次都压的是底注
			double lTableGoldTotal = m_lTableGoldTotal;
			double lTableGold[] = new double[5];
			for(int i = 0;i < m_lTableGold.length;i++){
				lTableGold[i] = m_lTableGold[i];
			}
			for(int i = 0;i < 5;i++){
				if (m_pIUserItem[i].dwUserID <= 0 || Constants.US_LOOKON == m_pIUserItem[i].cbUserStatus) {
					continue;
				}
				lTableGold[i] *= 1;//不成底注，扎金花每次都压的是底注
				BigDecimal b = new BigDecimal(lTableGold[i]);
				lTableGold[i] = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(); 
				SecShmUser user = dthall.getInstance().getSecurityUserInfo(m_pIUserItem[i].dwUserID);
				if(i != wWinnerUser && Math.abs(lTableGold[i]) > user.getRmb1v1(0.0)){
					//不会扣成负数
					lTableGold[i] = Math.abs(user.getRmb1v1(0));
				}
			}
			// 计算总注
			double lAllGold = 0;
			for (int i = 0; i < 5; i++)
				lAllGold += lTableGold[i];

			GameEnd.m_winnerSeat = wWinnerUser;
			for (int i = 0; i < 5; i++) {
				// 积分信息
				if (i == wWinnerUser) {
					vecGameGold[i] = lAllGold - lTableGold[i];
				} else {
					GameEnd.lUserCoin[i] = 0 - lTableGold[i];
					vecGameGold[i] = -lTableGold[i];
				}
			}
			//扣赢
			if(this.inHall() && m_dwcKouYing != 0 && m_dwcKouYingRate > 0){
				//不区别机器人还是真人
				for(int i = 0;i < 5;i++){
					if (m_pIUserItem[i].dwUserID <= 0 || Constants.US_LOOKON == m_pIUserItem[i].cbUserStatus) {
						continue;
					}
					//只扣赢家的，输家输的不变
					if(vecGameGold[i] > 0){
						double kou = vecGameGold[i] * m_dwcKouYingRate;
						BigDecimal b = new BigDecimal(kou);
						kou = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
						vecGameGold[i] -= kou;
						if(m_pIUserItem[i].isAndroidUser == 0){
							//只记录真人的
							UserRedisInfo.addKouYingFanXianForRent(m_pIUserItem[i].dwUserID,kou);
							double givesys = (1.0 - m_dwcKouYingFanliRate) * kou;
							double giveagent = m_dwcKouYingFanliRate * kou;
							BigDecimal b1 = new BigDecimal(givesys);
							givesys = b1.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
							BigDecimal b2 = new BigDecimal(giveagent);
							giveagent = b2.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
							UserRedisInfo.addKouYingFanXian(m_pIUserItem[i].dwUserID, giveagent);
							DwcRedisRecord.getInstance().addRmbGameInfo(UserRmbChange.reason16, givesys);
						}
					}
				}
			}

			for (int i = 0; i < 5; i++) {
				GameEnd.nGameGold[i] = vecGameGold[i];
				m_doubleVecResultScore[i] += vecGameGold[i];
				m_pIUserItem[i].lUserScore += vecGameGold[i];
				GameEnd.nUserScore[i] = m_doubleVecResultScore[i];
			}
			boolean isYeJi = true;
			if(this.inUserClub()){
				for(int i = 0;i < 5;i++){
					m_doubleKouWinResultScore[i] += vecGameGold[i];
				}
				this.m_kouWinCurrJu++;
				if(this.m_kouWinSumJu == this.m_kouWinCurrJu){
					//扣除业绩
					for(int i = 0;i < 5;i++){
						if (m_pIUserItem[i].dwUserID <= 0 || Constants.US_LOOKON == m_pIUserItem[i].cbUserStatus) {
							continue;
						}
						//只扣赢家的，输家输的不变
						if(m_doubleKouWinResultScore[i] > 0){
							double kou = m_doubleKouWinResultScore[i] * m_dwcKouYingRate;
							BigDecimal b = new BigDecimal(kou);
							kou = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
							m_doubleVecResultScore[i] -= kou;

							SecShmUser safeuser = dthall.getInstance().getSecurityUserInfo(m_pIUserItem[i].dwUserID);
							m_pIUserItem[i].lUserRmb -= kou;
							safeuser.getRmb1v1(0 - kou);
							
							if(m_pIUserItem[i].isAndroidUser == 0){
								//只记录真人的
								UserRedisInfo.addKouYingFanXianForRent(m_pIUserItem[i].dwUserID,kou);
								double givesys = (1.0 - m_dwcKouYingFanliRate) * kou;
								double giveagent = m_dwcKouYingFanliRate * kou;
								BigDecimal b1 = new BigDecimal(givesys);
								givesys = b1.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
								BigDecimal b2 = new BigDecimal(giveagent);
								giveagent = b2.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
								UserRedisInfo.addKouYingFanXian(m_pIUserItem[i].dwUserID, giveagent);
								DwcRedisRecord.getInstance().addRmbGameInfo(UserRmbChange.reason18, givesys);
							}
						}
					}
					this.m_kouWinCurrJu = 0;
					for(int i = 0;i < 5;i++){
						if (m_pIUserItem[i].dwUserID <= 0 || Constants.US_LOOKON == m_pIUserItem[i].cbUserStatus) {
							continue;
						}
						m_doubleKouWinResultScore[i] = 0.0;
					}
				}else{
					isYeJi = false;
				}
			}
			if(hasAutoDianKong()){
				int dianKongUid = getDianKongUid();
				int seat = FindSeat(dianKongUid);
				if(seat != Constants.INVALID_CHAIR){
					checkDianKong(m_doubleVecResultScore[seat]);
				}
			}
			//加入业绩
			for(int i = 0;i < 5;i++){
				if (m_pIUserItem[i].dwUserID <= 0 || Constants.US_LOOKON == m_pIUserItem[i].cbUserStatus) {
					continue;
				}
				if(m_pIUserItem[i].isAndroidUser == 0 && isYeJi){
					//只处理非机器人,正负都处理
					UserRedisInfo.addYeJiFanXian(m_pIUserItem[i].dwUserID,Math.abs(vecGameGold[i]),m_clubOwnerId);
				}
			}
			//广播
			for(int i = 0;i < 5;i++){
				if (m_pIUserItem[i].dwUserID <= 0 || Constants.US_LOOKON == m_pIUserItem[i].cbUserStatus) {
					continue;
				}
				if(i == wWinnerUser && lTableGold[i] > GoldMgr.getInstance().getRmbBroadCastLimit()){
					//大于进行广播
					net.sf.json.JSONObject broad = new net.sf.json.JSONObject();
					broad.put("command","public_logic_broad_user");
					broad.put("value1", m_pIUserItem[i].szNickName);
					broad.put("value2", "炸金花");
					broad.put("value3", lTableGold[i]);
					broad.put("value4", m_GameLogic.GetCardStringType(m_bTableCardArray[i], 3));
					AdminRedis.getInstance().pubAdminSubMsg(broad);
				}
			}
			//扣除
			for(int i = 0;i < 5;i++){
				if (m_pIUserItem[i].dwUserID <= 0 || Constants.US_LOOKON == m_pIUserItem[i].cbUserStatus) {
					continue;
				}
				SecShmUser safeUser = dthall.getInstance().getSecurityUserInfo(m_pIUserItem[i].dwUserID);
				m_pIUserItem[i].lUserRmb += vecGameGold[i];
				safeUser.getRmb1v1(vecGameGold[i]);
				UserRmbChange.handleRmbChangeWinLose(safeUser,vecGameGold[i], UserRmbChange.reason16);
				
				if(m_pIUserItem[i].isAndroidUser == 2){
					//robot win & lost
					this.addRobotWinScore(vecGameGold[i]);
				}
			}
			GameEnd.lShuiShou = realshui;
			for (int i = 0; i < 5; i++) {
				if (m_pIUserItem[i].dwUserID <= 0 || Constants.US_LOOKON == m_pIUserItem[i].cbUserStatus) {
					continue;
				}
				GameEnd.lUserCoin[i] = m_pIUserItem[i].lUserRmb;
			}
			// 发送信息
			SendTableData(Constants.INVALID_CHAIR, GameEnd.getJsonData());

			ConcludeGame();
			
			for (int i = 0; i < 5; i++) {
				SHMUser user1 = dthall.getInstance().getUserInfo(m_pIUserItem[i].dwUserID);
				if (m_pIUserItem[i].dwUserID <= 0 || Constants.US_LOOKON == m_pIUserItem[i].cbUserStatus) {
					continue;
				}
				if (user1 != null) {
					user1.getDwcJuShu(1);
				}
			}

			break;
		}
		case 0x02:// GER_USER_LEFT: //用户强退
		{
			// 强退处理
			OnUserFangQi(wChairID);
			break;
		}
		}

		if (m_byRound <= 2 && cbReason == GAME_END_DISMISS) {
			SUBCMD_S_DismissRoom cmd = new SUBCMD_S_DismissRoom();
			cmd.lUid = 0;
			cmd.cbResult = 0;
			cmd.m_wUserChairID = 0;
			cmd.m_dwTableID = m_dwTableID;

			SendTableData(Constants.INVALID_CHAIR, cmd.getJsonData());

			DelayRemove();
			beforeDestroy(cloneServerUserPack(m_pIUserItem),null,m_doubleVecResultScore);
			QpGameMgr.getInstance().OnDismissRoom(this);
			m_bTableStarted = false;
			m_jiesuan = true;
			for (int i = 0; i < 5; i++) {// 清除牌局数据
				m_vecResultScore[i] = 0;
			}

		} else if ((m_byRound > m_byMaxRound || cbReason == GAME_END_DISMISS)&&m_jiesuan == false) {
			if(m_byRound > m_byMaxRound){
				for(int i = 0;i < 5;i++){
					SHMUser user = dthall.getInstance().getUserInfo(m_pIUserItem[i].dwUserID);
					if(user != null){
						user.getDwcJuShu(1);
					}
				}
			}
			SUBCMD_S_REPORT cmd = new SUBCMD_S_REPORT();
			cmd.cbReason = cbReason;
			cmd.whoDismiss = m_WhoTryDismiss;

			for (int k = 0; k < 5; k++) {
				cmd.vecWinScore[k] = m_vecWinScore[k];
				cmd.vecLoseScore[k] = m_vecLoseScore[k];
				cmd.vecResultScore[k] = m_doubleVecResultScore[k];
			}

			SendTableData(Constants.INVALID_CHAIR, cmd.getJsonData());

			DelayRemove();

			beforeDestroy(cloneServerUserPack(m_pIUserItem),null,m_doubleVecResultScore);
			QpGameMgr.getInstance().OnDismissRoom(this);
		}

		return false;
	}
	public boolean onEventGameEndGold(int wChairID, int cbReason) {
		m_bGameStarted = false;
		m_byRound++;
		OnRoundInfoChange();
		m_wFaPaiDelay = 3;

		int cntUser = 0;
		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].dwUserID > 0 && m_pIUserItem[i].isAndroidUser == 0 && Constants.US_LOOKON != m_pIUserItem[i].cbUserStatus) {
				cntUser++;
			}
		}
		
		switch (cbReason) {
		case 0:// GER_NORMAL: //常规结束
		case 0x10:// GER_NO_PLAYER: //没有玩家
		{
			double shuishourate = getShuiShou(this.GetType());
			int shui = (int) (this.m_difen * shuishourate);
			double[] realshui = new double[5];
			for(int i = 0;i < 5;i++){
				realshui[i] = 0;
			}
			// 统计信息
			int wWinnerUser = DeduceWinner();

			Winner =  wWinnerUser;
			//扣税
			for(int i = 0;i < 5;i++){
				if (m_pIUserItem[i].dwUserID <= 0 || Constants.US_LOOKON == m_pIUserItem[i].cbUserStatus) {
					continue;
				}
				if(m_kouShuiMode.equals("win") && i != Winner){
					continue;
				}else if(m_kouShuiMode.equals("lose") && i == Winner){
					continue;
				}else{
					//全部扣税
				}
				realshui[i] = shui;
				SecShmUser user = dthall.getInstance().getSecurityUserInfo(m_pIUserItem[i].dwUserID);
				//扣除税收先,在扣除UU逗的时候，最多扣完,不会扣成负数(系统不补贴)
				m_pIUserItem[i].lUserCoin -= shui;
				user.getCoin(0 - shui);
				if(m_pIUserItem[i].isAndroidUser == 0){
					//正常用户
					UserGoldChange.handleGoldChangeKouShui(user, shui, UserGoldChange.reason6);
					this.addEatNormalScore(shui);
					handleGoldGiveBack(m_pIUserItem[i].dwUserID,shui);
				}else if(m_pIUserItem[i].isAndroidUser == 2){
					//机器人
					this.addEatRobotScore(shui);
				}
			}
			
			int vecGameGold[] = new int[5];
			// 定义变量
			SUBCMD_S_JieSuan GameEnd = new SUBCMD_S_JieSuan();


			//乘上底注
			m_lTableGoldTotal *= 1;
			for(int i = 0;i < 5;i++){
				if (m_pIUserItem[i].dwUserID <= 0 || Constants.US_LOOKON == m_pIUserItem[i].cbUserStatus) {
					continue;
				}
				m_lTableGold[i] *= 1;
				SecShmUser user = dthall.getInstance().getSecurityUserInfo(m_pIUserItem[i].dwUserID);
				if(i != wWinnerUser && Math.abs(m_lTableGold[i]) > user.getCoin(0)){
					//不会扣成负数
					m_lTableGold[i] = Math.abs(user.getCoin(0));
				}
			}
			// 计算总注
			int lAllGold = 0;
			for (int i = 0; i < 5; i++)
				lAllGold += m_lTableGold[i];

			GameEnd.m_winnerSeat = wWinnerUser;
			for (int i = 0; i < 5; i++) {
				// 积分信息
				if (i == wWinnerUser) {
					vecGameGold[i] = lAllGold - m_lTableGold[i];
				} else {
					GameEnd.nGameGold[i] = 0 - m_lTableGold[i];

					vecGameGold[i] = -m_lTableGold[i];
				}
			}
			// 结束游戏

			for (int i = 0; i < 5; i++) {
				GameEnd.nGameGold[i] = vecGameGold[i];
				m_vecResultScore[i] += vecGameGold[i];
				m_pIUserItem[i].lUserScore += vecGameGold[i];
				GameEnd.nUserScore[i] = m_pIUserItem[i].lUserScore;
			}
			//扣除
			for(int i = 0;i < 5;i++){
				if (m_pIUserItem[i].dwUserID <= 0 || Constants.US_LOOKON == m_pIUserItem[i].cbUserStatus) {
					continue;
				}
				SecShmUser safeUser = dthall.getInstance().getSecurityUserInfo(m_pIUserItem[i].dwUserID);
				m_pIUserItem[i].lUserCoin += vecGameGold[i];
				safeUser.getCoin(vecGameGold[i]);
				UserGoldChange.handleGoldChangeWinLose(safeUser,vecGameGold[i], UserGoldChange.reason16);
				
				if(m_pIUserItem[i].isAndroidUser == 2){
					//robot win & lost
					this.addRobotWinScore(vecGameGold[i]);
				}
			}
			GameEnd.lShuiShou = realshui;
			for (int i = 0; i < 5; i++) {
				if (m_pIUserItem[i].dwUserID <= 0 || Constants.US_LOOKON == m_pIUserItem[i].cbUserStatus) {
					continue;
				}
				GameEnd.lUserCoin[i] = m_pIUserItem[i].lUserCoin;
			}
			// 发送信息
			SendTableData(Constants.INVALID_CHAIR, GameEnd.getJsonData());

			ConcludeGame();
			
			for (int i = 0; i < 5; i++) {
				if (m_pIUserItem[i].dwUserID <= 0 || Constants.US_LOOKON == m_pIUserItem[i].cbUserStatus) {
					continue;
				}
				SHMUser user1 = dthall.getInstance().getUserInfo(m_pIUserItem[i].dwUserID);
				if (user1 != null) {
					user1.getGoldJuShu(1);
				}
			}

			break;
		}
		case 0x02:// GER_USER_LEFT: //用户强退
		{
			// 强退处理
			OnUserFangQi(wChairID);
			break;
		}
		}

		if (m_byRound <= 2 && cbReason == GAME_END_DISMISS) {
			SUBCMD_S_DismissRoom cmd = new SUBCMD_S_DismissRoom();
			cmd.lUid = 0;
			cmd.cbResult = 0;
			cmd.m_wUserChairID = 0;
			cmd.m_dwTableID = m_dwTableID;

			SendTableData(Constants.INVALID_CHAIR, cmd.getJsonData());

			DelayRemove();
			beforeDestroy(cloneServerUserPack(m_pIUserItem),m_vecResultScore,null);
			QpGameMgr.getInstance().OnDismissRoom(this);
			m_bTableStarted = false;
			m_jiesuan = true;
			for (int i = 0; i < 5; i++) {// 清除牌局数据
				m_vecResultScore[i] = 0;
			}

		} else if ((m_byRound > m_byMaxRound || cbReason == GAME_END_DISMISS)&&m_jiesuan == false) {
			if(m_byRound > m_byMaxRound){
				for(int i = 0;i < 5;i++){
					SHMUser user = dthall.getInstance().getUserInfo(m_pIUserItem[i].dwUserID);
					if(user != null){
						user.getGoldJuShu(1);
					}
				}
			}
			SUBCMD_S_REPORT cmd = new SUBCMD_S_REPORT();
			cmd.cbReason = cbReason;
			cmd.whoDismiss = m_WhoTryDismiss;

			for (int k = 0; k < 5; k++) {
				cmd.vecWinScore[k] = m_vecWinScore[k];
				cmd.vecLoseScore[k] = m_vecLoseScore[k];
				cmd.vecResultScore[k] = m_vecResultScore[k];
			}

			SendTableData(Constants.INVALID_CHAIR, cmd.getJsonData());

			DelayRemove();

			beforeDestroy(cloneServerUserPack(m_pIUserItem),m_vecResultScore,null);
			QpGameMgr.getInstance().OnDismissRoom(this);
		}

		return false;
	}
	// 游戏结束
	public boolean OnEventGameEndZuJu(int wChairID, int cbReason) {
		m_bGameStarted = false;
		m_byRound++;
		OnRoundInfoChange();
		m_wFaPaiDelay = 3;
		switch (cbReason) {
		case 0:// GER_NORMAL: //常规结束
		case 0x10:// GER_NO_PLAYER: //没有玩家
		{
			int vecGameGold[] = new int[5];
			// 定义变量
			SUBCMD_S_JieSuan GameEnd = new SUBCMD_S_JieSuan();
			// 计算总注
			int lAllGold = 0;
			
			for (int i = 0; i < 5; i++)
				lAllGold += m_lTableGold[i];
			if (lAllGold < m_lTableGoldTotal) {
				lAllGold = (int)m_lTableGoldTotal;
			}

			// 统计信息
			int wWinnerUser = DeduceWinner();

			Winner =  wWinnerUser;

			GameEnd.m_winnerSeat = wWinnerUser;
			for (int i = 0; i < 5; i++) {
				// 积分信息
				if (i == wWinnerUser) {
					vecGameGold[i] = lAllGold - m_lTableGold[i];
				} else {
					GameEnd.nGameGold[i] = 0 - m_lTableGold[i];

					vecGameGold[i] = -m_lTableGold[i];
				}
			}
			// 结束游戏

			for (int i = 0; i < 5; i++) {
				GameEnd.nGameGold[i] = vecGameGold[i];
				m_vecResultScore[i] += vecGameGold[i];
				m_pIUserItem[i].lUserScore += vecGameGold[i];
				GameEnd.nUserScore[i] = m_pIUserItem[i].lUserScore;
			}

			// 发送信息
			SendTableData(Constants.INVALID_CHAIR, GameEnd.getJsonData());

			ConcludeGame();
			
			for (int i = 0; i < 5; i++) {
				SHMUser user1 = dthall.getInstance().getUserInfo(m_pIUserItem[i].dwUserID);
				if (user1 != null) {
					user1.getZuJuJuShu(1);
				}
			}

			/*
			 * for (int i = 0; i < 5; i++) { if (m_pIUserItem[i].dwUserID &&
			 * m_cntNotReady[i] > 3) { Kick(m_pIUserItem[i].dwUserID);
			 * m_cntNotReady[i] = 0; } }
			 */
			break;
		}
		case 0x02:// GER_USER_LEFT: //用户强退
		{
			// 强退处理
			OnUserFangQi(wChairID);
			break;
		}
		}

		if (m_byRound <= 2 && cbReason == GAME_END_DISMISS) {
			SUBCMD_S_DismissRoom cmd = new SUBCMD_S_DismissRoom();
			cmd.lUid = 0;
			cmd.cbResult = 0;
			cmd.m_wUserChairID = 0;
			cmd.m_dwTableID = m_dwTableID;

			SendTableData(Constants.INVALID_CHAIR, cmd.getJsonData());

			DelayRemove();
			QpGameMgr.getInstance().OnDismissRoom(this);
			m_bTableStarted = false;
			m_jiesuan = true;
			for (int i = 0; i < 5; i++) {// 清除牌局数据
				m_vecResultScore[i] = 0;
			}

		} else if ((m_byRound > m_byMaxRound || cbReason == GAME_END_DISMISS)&&m_jiesuan == false) {
			if(m_byRound > m_byMaxRound){
				for(int i = 0;i < 5;i++){
					SHMUser user = dthall.getInstance().getUserInfo(m_pIUserItem[i].dwUserID);
					if(user != null){
						user.getZuJuJuShu(1);
					}
				}
			}
			int before_cost[] = new int[5];
			int after_cost[] = new int[5];

			int cost[] = new int[5];			
			for(int i=0;i<5;i++)
			{
				before_cost[i] = 0;
				cost[i] = 0;
				after_cost[i] = 0;
			}
			
			for (int i = 0; i < 5; i++) 
			{				
				if(m_pIUserItem[i].dwUserID > 0)
				{
					SHMUser user = dthall.getInstance().getUserInfo(m_pIUserItem[i].dwUserID);
					if(user != null)
					{
						before_cost[i] = user.getGem(0);
					}
				}
			}			
			int minRound = 1;
			if(m_byMaxRound > 8)
			{
				minRound = 4;
			}			
			if (m_byRound > minRound) {

				if (m_payWay.equalsIgnoreCase("FZPAY")) {
					int cost_gem = 0;
					for (int i = 0; i < 5; i++) {
						if (m_pIUserItem[i].dwUserID > 0) {
							cost_gem += m_byCostGem;
						}
					}
					if (m_Creator > 0) {
						SHMUser user = dthall.getInstance().getUserInfo(m_Creator);
						if (user != null) {
							user.getGem(0 - cost_gem);
						}
					} else {
						SHMUser user = dthall.getInstance().getUserInfo(m_pIUserItem[0].dwUserID);
						if (user != null) {
							user.getGem(0 - cost_gem);
						}
						cost[0] = 0 - cost_gem;
					}
				}
				
				if (m_payWay == "AAPAY") {
					for (int i = 0; i < 5; i++) {
						int uid = m_pIUserItem[i].dwUserID;
						if (uid > 0) {
							SHMUser user = dthall.getInstance().getUserInfo(uid);
							if (user != null) {
								user.getGem(0 - m_byCostGem);
								cost[i] = 0 - m_byCostGem;
							}
						}
					}
				}
				if (m_payWay.equalsIgnoreCase("WINPAY")) {
					int maxUser = 5;
					int activeUser = 0;
					for(int i = 0;i < maxUser;i++){
						if (m_pIUserItem[i].dwUserID > 0){
							activeUser++;
						}
					}
					int maxcost = m_byCostGem * activeUser;
					List<Integer> winuser = new ArrayList<Integer>();
					int max = -1;
					for (int i = 0; i < maxUser; i++) {
						if (m_vecResultScore[i] > max) {
							max = m_vecResultScore[i];
						}
					}
					for (int i = 0; i < maxUser; i++) {
						if (m_vecResultScore[i] == max) {
							winuser.add(i);
						}
					}
					float rate = m_byRound * 1.0f / m_byMaxRound;
					if(rate > 1.0) rate = 1.0f;
					rate *= maxcost;
					Float frate = new Float(rate);
					int cost_gem = frate.intValue();
					cost_gem /= winuser.size();

					for (int i = 0; i < maxUser; i++) {
						if (m_pIUserItem[i].dwUserID > 0 && winuser.indexOf(i) != -1) {
							SHMUser user = dthall.getInstance().getUserInfo(m_pIUserItem[i].dwUserID);
							if (user != null) {
								user.getGem(0 - cost_gem);
								cost[i] = 0 - cost_gem;
							}
						}
					}
				}
				if (m_payWay.equalsIgnoreCase("CLUBPAY")) {
					int cost_gem = 0;
					for (int i = 0; i < 5; i++) {
						if (m_pIUserItem[i].dwUserID > 0) {
							cost_gem += m_byCostGem;
						}
					}

					DbInfoMgr.getInstance().costClubOwnerGem(this.m_clubOwnerId,cost_gem);
				}
				
				for (int i = 0; i < 5; i++) 
				{
					if(m_pIUserItem[i].dwUserID > 0)
					{
						SHMUser user = dthall.getInstance().getUserInfo(m_pIUserItem[i].dwUserID);
						if(user != null)
						{
							after_cost[i] = user.getGem(0);
						}
					}
				}					
			}
				
			SUBCMD_S_REPORT cmd = new SUBCMD_S_REPORT();
			cmd.cbReason = cbReason;
			cmd.whoDismiss = m_WhoTryDismiss;

			for (int k = 0; k < 5; k++) {
				cmd.vecWinScore[k] = m_vecWinScore[k];
				cmd.vecLoseScore[k] = m_vecLoseScore[k];
				cmd.vecResultScore[k] = m_vecResultScore[k];
			}

			SendTableData(Constants.INVALID_CHAIR, cmd.getJsonData());
			for (int i = 0; i < 5; i++) {
				if (m_pIUserItem[i].dwUserID > 0) {
					SHMUser user = dthall.getInstance().getUserInfo(m_pIUserItem[i].dwUserID);
					if(user != null)
					{
						user.getWinScore(m_vecResultScore[i]);
					}
				}
			}
			log_counter++;
			log_counter = log_counter % 100;
			Long t = System.currentTimeMillis() / 1000;
			Long battle_id = (t % 1000000) * 100 + log_counter;

			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String created = df.format(new Date());
			{

				JSONObject root = new JSONObject();

				root.put("command", "save_battle_log");				
				root.put("battle_id", battle_id);
				
				root.put("room_type", GetType());

				root.put("room_id", m_dwTableID);
				root.put("creator", GetCreator());
				
				root.put("jushu", (int) m_byMaxRound);
				root.put("limitscore", m_lRoundLimit);
				root.put("difen", m_diFen);
				
				if (m_payWay == "AAPAY") {
					root.put("payway", 1);	
				}else if (m_payWay == "FZPAY") {
					root.put("payway", 2);
				}else{
					root.put("payway", 0);
				}
				root.put("created", created);
				root.put("uid0", (int) m_pIUserItem[0].dwUserID);
				root.put("nickname0", m_pIUserItem[0].szNickName);
				root.put("score0", m_vecResultScore[0]);
				root.put("cost0", cost[0]);
				root.put("uid1", (int) m_pIUserItem[1].dwUserID);
				root.put("nickname1", m_pIUserItem[1].szNickName);
				root.put("score1", m_vecResultScore[1]);
				root.put("cost1", cost[1]);
				root.put("uid2", (int) m_pIUserItem[2].dwUserID);
				root.put("nickname2", m_pIUserItem[2].szNickName);
				root.put("score2", m_vecResultScore[2]);
				root.put("cost2", cost[2]);
				root.put("uid3", (int) m_pIUserItem[3].dwUserID);
				root.put("nickname3", m_pIUserItem[3].szNickName);
				root.put("score3", m_vecResultScore[3]);
				root.put("cost3", cost[3]);
				root.put("uid4", (int) m_pIUserItem[4].dwUserID);
				root.put("nickname4", m_pIUserItem[4].szNickName);
				root.put("score4", m_vecResultScore[4]);
				root.put("cost4", cost[4]);

				root.put("headurl0", dthall.getInstance().Handle_OnGetUserAvatar(m_pIUserItem[0].dwUserID));
				root.put("headurl1", dthall.getInstance().Handle_OnGetUserAvatar(m_pIUserItem[1].dwUserID));
				root.put("headurl2", dthall.getInstance().Handle_OnGetUserAvatar(m_pIUserItem[2].dwUserID));
				root.put("headurl3", dthall.getInstance().Handle_OnGetUserAvatar(m_pIUserItem[3].dwUserID));
				root.put("headurl4", dthall.getInstance().Handle_OnGetUserAvatar(m_pIUserItem[4].dwUserID));
				
				GameDB.getInstance().push_task(null, root);
				try
				{
					for(int i =0;i<5;i++)
					{
						if(m_pIUserItem[i].dwUserID > 0 && cost[i] != 0)
						{
							JSONObject root_cost = new JSONObject();
							root_cost.put("command", "save_charge_log");
							root_cost.put("created", created);
							root_cost.put("uid", (int) m_pIUserItem[i].dwUserID);
							root_cost.put("before_cost", (int) before_cost[i]);
							root_cost.put("after_cost", (int) after_cost[i]);
							root_cost.put("cost", (int) cost[i]);
							root_cost.put("reason", "用户" + m_pIUserItem[0].dwUserID + "创建  诈金花  " + m_byMaxRound + "局 " + m_payWay + " " + m_lRoundLimit + "封顶" + " 底分 " + m_diFen);
							root_cost.put("room_id", m_dwTableID);
							
							GameDB.getInstance().push_task(null, root_cost);
						}
					}	
				}catch(Exception e)
				{
					e.printStackTrace();
				}


			}


			for (int i = 0; i < 5; i++) {// 清除牌局数据
				m_vecResultScore[i] = 0;
			}

			DelayRemove();

			QpGameMgr.getInstance().OnDismissRoom(this);

			//处理积分
			if(m_byRound == m_byMaxRound){
				int score = 1;
				for(int i = 0;i < 5;i++){
					if(m_pIUserItem[i].dwUserID > 0){
						int uid = m_pIUserItem[i].dwUserID;
						UserActionActivity activity = ActivityMgr.getInstance().handleSendHuaFeiActionByPlay(uid,score,"*");
						if(activity.m_useActioned){
							RankMgr.getInstance().addUserRankScore(uid,activity.m_expGet);
						}
					}
				}
			}
		}

		return false;
	}
	public void SetDesc(String szDesc) {
		m_strRoomDesc = szDesc;
	}
	// 发送场景
	public boolean SendGameScene(int _Uid, int bGameStatus) {
		if (m_strParam.length() > 0) {
			SUBCMD_S_Param cmd = new SUBCMD_S_Param();
			cmd.m_dwTableID = m_dwTableID;
			cmd.szParam = m_strParam;
			cmd.szRoomDesc = m_strRoomDesc;			
			cmd.m_ldifen = m_diFen;
			cmd.m_creater = m_Creator;
			if(this.isDwcType() && this.m_dwcKouYing != 0){
				cmd.dwcKouYingRate = m_dwcKouYingRate;
			}

			if(this.inHall()){
				cmd.m_kouYingJuShu = 0;
			}else{
				cmd.m_kouYingJuShu = GoldMgr.getInstance().getKouWinJuShuInClub();
			}
			SendData(_Uid, cmd.getJsonData());
		}
		{
			SUBCMD_S_Table cmd = new SUBCMD_S_Table();
			cmd.m_dwTableID = m_dwTableID;// 房间ID
			cmd.m_bTableStarted =  (m_bTableStarted ? 1 : 0);// 是否加密
			cmd.m_bGameStarted =  (m_bGameStarted ? 1 : 0);// 是否开始
			cmd.m_lCellScore = m_lCellScore;
			cmd.m_byRound = m_byRound;//
			cmd.m_byMaxRound = m_byMaxRound;//
			cmd.m_wCurrentUser = m_wCurrentUser;
			cmd.m_curRoomMultiple = m_lTurnBasicGold;

			for (int i = 0; i < 5; i++) {
				cmd.m_seatInfo[i].m_lUid = m_pIUserItem[i].dwUserID;// UID
				cmd.m_seatInfo[i].m_lTotalScore = m_pIUserItem[i].lUserScore;// COIN
				cmd.m_seatInfo[i].m_NickName = m_pIUserItem[i].szNickName;// NICKNAME
				cmd.m_seatInfo[i].m_lBetCoin = m_lTableGold[i];//
				cmd.m_seatInfo[i].m_cbGender =  m_pIUserItem[i].cbGender;
				cmd.m_seatInfo[i].m_iAvatar = m_pIUserItem[i].dwFaceID;//
				cmd.m_seatInfo[i].m_cbUserStatus =  m_pIUserItem[i].cbUserStatus;// 用户状态
				cmd.m_seatInfo[i].m_iVipLevel =  m_pIUserItem[i].cbVipLevel;// VIP
																					// LEVEL
				cmd.m_seatInfo[i].m_iTitle = 0;// 称号
				cmd.m_seatInfo[i].m_isLookon = 0;// 是否观众
				cmd.m_seatInfo[i].szIP = m_pIUserItem[i].szIP;
				cmd.m_seatInfo[i].m_iTotalCoin = m_pIUserItem[i].lUserCoin;
				if(this.isDwcType()){
					cmd.m_seatInfo[i].m_iTotalRmb = m_pIUserItem[i].lUserRmb;
				}
			}

			SendData(_Uid, cmd.getJsonData());

			for (int i = 0; i < 5; i++) {
				if (m_pIUserItem[i].dwUserID > 0) {
					SendAvatar(m_pIUserItem[i].dwUserID);
				}
			}
		}

		if (m_bTableStarted == true) {
			SUBCMD_S_STAGE cmd = new SUBCMD_S_STAGE();
			cmd.szStage = m_curStage;
			cmd.m_byZhuang = m_byZhuang;// 当前庄位置
			cmd.m_wZhuangPower = 1;// 当前庄的倍率
			cmd.needDealTime = m_byZhuang;
			for (int i = 0; i < 5; i++) {
				for (int k = 0; k < 3; k++) {
					cmd.m_bTableCardArray[i * 3 + k] = m_bTableCardArray[i][k]; // 桌面扑克
																				// //been
																				// 5
				}
				cmd.m_vecCardType[i] = m_vecCardType[i];
				cmd.m_vecRoundScore[i] = m_vecRoundScore[i];
			}

			SendData(_Uid, cmd.getJsonData());
		}
		if (m_bGameStarted == true) {
			SUBCMD_S_CardStatus cmd = new SUBCMD_S_CardStatus();
			for (int i = 0; i < 5; i++) {
				cmd.m_cbCardStatus[i] = m_cbCardStatus[i];
			}
		}

		switch (bGameStatus) {
		case 0:// GS_FREE: //空闲状态
		{
			// 构造数据
			SUBCMD_S_StatusFree cmd = new SUBCMD_S_StatusFree();
			cmd.m_lCellScore = m_lCellScore;

			// 发送场景

			SendData(_Uid, cmd.getJsonData());

			return true;
		}
		case 100:// GS_PLAYING: //游戏状态
		{
			// 构造数据
			SUBCMD_S_StatusPlay cmd = new SUBCMD_S_StatusPlay();
			cmd.m_lCellScore = m_lCellScore;
			for (int i = 0; i < 5; i++) {
				cmd.m_cntTableChip[i] = m_cntTableChip[i];// 1x,3x,5x,8x,10x
			}
			for (int i = 0; i < 5; i++) {
				cmd.m_cbCardStatus[i] = m_cbCardStatus[i];
				cmd.m_AllBetCoin[i] = m_lTableGold[i];// 底注
				cmd.m_AllUserCoin[i] = m_pIUserItem[i].lUserScore;// 每个玩家多少钱
			}

			// 发送场景

			SendData(_Uid, cmd.getJsonData());

			int seat = FindSeat(_Uid);
			if(m_cbCardStatus[seat] == Constants.CS_KANPAI){
				SUBCMD_S_KanPai kancmd = new SUBCMD_S_KanPai();
				kancmd.m_lUid = _Uid;
				kancmd.m_wUserChairID = seat; // 玩家座位号
				kancmd.wCurrentUser = (int) m_wCurrentUser; // 当前用户
				for (int i = 0; i < 3; i++) {
					kancmd.bCardData[i] = m_bTableCardArray[seat][i]; // 用户扑克
				}

				SendTableData(seat, kancmd.getJsonData());
			}
			return true;
		}
		}

		return false;
	}

	public boolean SendTableData(int wChairID, String sData) {
		System.out.println(sData);
		// 群发用户
		if (wChairID == Constants.INVALID_CHAIR) {
			for (int i = 0; i < 5; i++) {
				if (m_pIUserItem[i].dwUserID > 0) {
					if (m_pIUserItem[i].isAndroidUser == 0) {
						dthall.getInstance().onSend(m_pIUserItem[i].dwUserID, sData);
					} else {
						// mPlayers[i]->SendGameMessage(wSubCmdID, pData,
						// wDataSize, data);
					}
				}
			}

			return true;
		} else {
			if (wChairID >= 5)
				return true;
			if (m_pIUserItem[wChairID].dwUserID > 0) {
				if (m_pIUserItem[wChairID].isAndroidUser == 0) {
					dthall.getInstance().onSend(m_pIUserItem[wChairID].dwUserID, sData);
				} else {
					// mPlayers[wChairID]->SendGameMessage(wSubCmdID, pData,
					// wDataSize, data);
				}
				return true;
			}
		}

		return false;
	}

	public boolean SendData(int wChairID, String sData) {
		// 群发用户
		if (wChairID == Constants.INVALID_CHAIR) {
			for (int i = 0; i < 5; i++) {
				if (m_pIUserItem[i].dwUserID > 0) {
					if (m_pIUserItem[i].isAndroidUser == 0) {
						dthall.getInstance().onSend(m_pIUserItem[i].dwUserID, sData);
					} else {
						// mPlayers[i]->SendGameMessage(wSubCmdID, pData,
						// wDataSize, data);
					}
				}
			}

			return true;
		} else {
			if (wChairID >= 5) {
				dthall.getInstance().onSend(wChairID, sData);
			} else if (m_pIUserItem[wChairID].dwUserID > 0) {
				if (m_pIUserItem[wChairID].isAndroidUser == 0) {
					dthall.getInstance().onSend(m_pIUserItem[wChairID].dwUserID, sData);
				} else {
					// mPlayers[wChairID]->SendGameMessage(wSubCmdID, pData,
					// wDataSize, data);
				}
				return true;
			}
		}

		return false;
	}

	// 就绪事件
	protected boolean OnUserReady(int wChairID) {
		if (wChairID >= 5) {
			System.out.printf("Error wChairID (%d)\n", wChairID);
			return false;
		}
		if(m_pIUserItem[wChairID].cbUserStatus == US_READY){
			return false;
		}
		m_cntNotReady[wChairID] = 0;
		m_wRobotSeconds[wChairID] = 0;
		m_cbCardStatus[wChairID] =  Constants.CS_NULL;
		// 状态判断
		if (m_bGameStarted)
			return false;

		m_pIUserItem[wChairID].cbUserStatus = US_READY;

		SUBCMD_S_Ready cmd = new SUBCMD_S_Ready();
		cmd.m_lUid = m_pIUserItem[wChairID].dwUserID;
		cmd.m_wUserChairID = (int) wChairID;// 玩家座位号

		SendTableData(Constants.INVALID_CHAIR, cmd.getJsonData());

		return false;
	}

	// 放弃事件
	protected boolean OnUserFangQi(int wChairID) {

		if (wChairID >= 5) {
			System.out.printf("Error wChairID (%d)\n", wChairID);
			return false;
		}
		// 状态判断
		if (m_pIUserItem[wChairID].cbUserStatus != US_PLAY)
			return false;

		m_wRobotSeconds[wChairID] = 0;
		m_cbCardStatus[wChairID] =  Constants.CS_QIPAI;

		// 设置数据
		m_pIUserItem[wChairID].cbUserStatus = US_NULL;
		m_bTableCardCount[wChairID] = 0;

		// 发送放弃消息
		SUBCMD_S_FangQi cmd = new SUBCMD_S_FangQi();
		cmd.m_lUid = m_pIUserItem[wChairID].dwUserID;
		cmd.m_wUserChairID = (int) wChairID;
		SendTableData(Constants.INVALID_CHAIR, cmd.getJsonData());

		// 判断结束
		int wPlayerCount = 0;
		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].cbUserStatus == US_PLAY)
				wPlayerCount++;
		}
		if (wPlayerCount >= 2) {
			if (m_wCurrentUser == wChairID)
				OnUserJiaZhu(wChairID, 0);
		} else {
			if (m_bGameStarted) {
				OnEventGameEnd(Constants.INVALID_CHAIR, Constants.GER_NO_PLAYER);
			}			
		}
			

		return false;
	}

	// 加注事件
	protected boolean OnUserJiaZhu(int wChairID, int lGold) {

		if (wChairID >= 5) {
			System.out.printf("Error wChairID (%d)\n", wChairID);
			return false;
		}
		if (m_wCurrentUser != wChairID) {
			System.out.printf("[1]OnUserJiaZhu Error m_wCurrentUser != ChairID(%d)(%d)\n", m_wCurrentUser, wChairID);
			return false;
		}
		if(this.isZuJuType()){
			if (lGold > 0 && (m_pIUserItem[wChairID].lUserScore - m_lTableGold[wChairID] < m_lTurnBasicGold * 4
					|| m_lTableGold[wChairID] >= m_lRoundLimit)) {
				return false;
			}
		}else if(this.isGoldType()){
			if(lGold > 0 && m_lTableGold[wChairID] >= m_lRoundLimit)
				return false;
		}else if(this.isDwcType()){
			if(lGold > 0 && m_lTableGold[wChairID] >= m_lRoundLimit)
				return false;			
		}
		m_wRobotSeconds[wChairID] = 0;
		// 设置数据
		if (lGold == 0) {
			// 效验数据
			if (m_wCurrentUser != wChairID)
				return false;

			// 计算限注

		}
		
		if (m_lCellScore > 0 && lGold > 0) {
			int power = (int) (lGold / m_lCellScore);

			switch (power) {
			case 1:
				m_cntTableChip[0] += 1;
				break;
			case 2:
				m_cntTableChip[1] += 1;
				break;
			case 3:
				m_cntTableChip[2] += 1;
				break;
			case 4:
				m_cntTableChip[3] += 1;
				break;
			case 5:
				m_cntTableChip[4] += 1;
				break;
			default:
				System.out.printf("INVALID ADDGOLD[%d] \n", lGold);
				return false;
			}
			/*
			 * switch (power) { case 1: m_cntTableChip[0] += 1; break; case 3:
			 * m_cntTableChip[1] += 1; break; case 5: m_cntTableChip[2] += 1;
			 * break; case 8: m_cntTableChip[3] += 1; break; case 10:
			 * m_cntTableChip[4] += 1; break; default: printf(
			 * "INVALID ADDGOLD[%d] \n", lGold); return false; }
			 */
		}
		
		m_cntGenZhu[wChairID]++;
		
		if (lGold > 0) {
			m_lTurnBasicGold = lGold;

			if (m_bKanPai[wChairID]) {
				m_lTableGold[wChairID] += m_lTurnBasicGold * 2;
				m_lTableGoldTotal += m_lTurnBasicGold * 2;
			} else {
				m_lTableGold[wChairID] += m_lTurnBasicGold;
				m_lTableGoldTotal += m_lTurnBasicGold;
			}
		}

		int wNextUser = Constants.INVALID_CHAIR; // 顺时针找下家，逆时针找下家应写成：for(i=6-1;i>=0;i--)
													// wNextUser=(m_wCurrentUser+i)%5;
		boolean isLastUser = true;
		for(int i = 0;i < 5;i++){
			wNextUser = (m_wCurrentUser + i) % 5;
			if (wNextUser > m_wCurrentUser) {
				if (m_pIUserItem[wNextUser].cbUserStatus == US_PLAY) {
					isLastUser = false;
					break;
				}
			}
		}
		if(isLastUser){
			m_chipRound++;
		}
		boolean bFindNext = false;
		for (int i = 0; i < 5; i++) {
			wNextUser = (m_wCurrentUser + i) % 5;
			if (wNextUser != m_wCurrentUser) {
				if (m_pIUserItem[wNextUser].cbUserStatus == US_PLAY) {
					bFindNext = true;
					break;
				}
			}
		}
		if (bFindNext) {
			// 设置变量
			m_wCurrentUser = wNextUser;

			SUBCMD_S_JiaZhu cmd = new SUBCMD_S_JiaZhu();
			cmd.m_lUid = m_pIUserItem[wChairID].dwUserID;
			cmd.m_wUserChairID = wChairID;
			cmd.m_lGold = lGold;
			cmd.m_bKanPai = (int) (m_bKanPai[wChairID] ? 1 : 0);
			cmd.wLastChairID = (int) wChairID;
			cmd.wCurrentUser = (int) m_wCurrentUser;
			cmd.m_allTableGold = (int)m_lTableGoldTotal;
			cmd.m_curUserdifen = m_lTableGold[wChairID];
			SendTableData(Constants.INVALID_CHAIR, cmd.getJsonData());
		} else {
			if (m_bGameStarted) {
				OnEventGameEnd(Constants.INVALID_CHAIR, Constants.GER_NO_PLAYER);
			}
		}

		return false;
	}

	// 跟注事件
	protected boolean OnUserGenZhu(int wChairID) {
		if (wChairID >= 5) {
			System.out.printf("Error wChairID (%d)\n", wChairID);
			return false;
		}
		if (m_wCurrentUser != wChairID) {
			System.out.printf("[1]OnUserGenZhu Error m_wCurrentUser != ChairID(%d)(%d)\n", m_wCurrentUser, wChairID);
			return false;
		}
		if(this.isZuJuType()){
			if (m_pIUserItem[wChairID].lUserScore - m_lTableGold[wChairID] < m_lTurnBasicGold * 4
					|| m_lTableGold[wChairID] >= m_lRoundLimit) {
				return false;
			}
		}else if(this.isGoldType()){
			if(m_lTableGold[wChairID] >= m_lRoundLimit)
				return false;
		}else if(this.isDwcType()){
			if(m_lTableGold[wChairID] >= m_lRoundLimit)
				return false;
		}
		m_wRobotSeconds[wChairID] = 0;

		// 效验数据
		if (m_wCurrentUser != wChairID)
			return false;

		
		if (m_lCellScore > 0 && m_lTurnBasicGold > 0) {
			int power = (int) (m_lTurnBasicGold / m_lCellScore);
			switch (power) {
			case 1:
				m_cntTableChip[0] += 1;
				break;
			case 2:
				m_cntTableChip[1] += 1;
				break;
			case 3:
				m_cntTableChip[2] += 1;
				break;
			case 4:
				m_cntTableChip[3] += 1;
				break;
			case 5:
				m_cntTableChip[4] += 1;
				break;
			default:
				System.out.printf("INVALID ADDGOLD[%d] \n", m_lTurnBasicGold);
				return false;
			}
			/*
			 * switch (power) { case 1: m_cntTableChip[0] += 1; break; case 3:
			 * m_cntTableChip[1] += 1; break; case 5: m_cntTableChip[2] += 1;
			 * break; case 8: m_cntTableChip[3] += 1; break; case 10:
			 * m_cntTableChip[4] += 1; break; default: printf(
			 * "INVALID ADDGOLD[%d] \n", m_lTurnBasicGold); return false; }
			 */
		}
		m_cntGenZhu[wChairID]++;
		if (m_bKanPai[wChairID]) {
			m_lTableGold[wChairID] += m_lTurnBasicGold * 2;
			m_lTableGoldTotal += m_lTurnBasicGold * 2;
		} else {
			m_lTableGold[wChairID] += m_lTurnBasicGold;
			m_lTableGoldTotal += m_lTurnBasicGold;
		}
		int wNextUser = Constants.INVALID_CHAIR; // 顺时针找下家，逆时针找下家应写成：for(i=6-1;i>=0;i--)
													// wNextUser=(m_wCurrentUser+i)%5;
		boolean bFindNext = false;
		for (int i = 0; i < 5; i++) {
			wNextUser = (m_wCurrentUser + i) % 5;
			if (wNextUser != m_wCurrentUser) {
				if (m_pIUserItem[wNextUser].cbUserStatus == US_PLAY) {
					bFindNext = true;
					break;
				}
			}
		}
		if (bFindNext) {
			// 设置变量
			m_wCurrentUser = wNextUser;

			SUBCMD_S_GenZhu cmd = new SUBCMD_S_GenZhu();
			cmd.m_lUid = m_pIUserItem[wChairID].dwUserID;
			cmd.m_wUserChairID = wChairID;
			cmd.m_lGold = m_lTurnBasicGold;
			cmd.m_bKanPai =  (m_bKanPai[wChairID] ? 1 : 0);
			cmd.wLastChairID = (int) wChairID;
			cmd.wCurrentUser = (int) m_wCurrentUser;
			cmd.m_allTableGold = (int)m_lTableGoldTotal;
			cmd.m_curUserdifen = m_lTableGold[wChairID];
			SendTableData(Constants.INVALID_CHAIR, cmd.getJsonData());
		} else {
			if (m_bGameStarted) {
				OnEventGameEnd(Constants.INVALID_CHAIR, Constants.GER_NO_PLAYER);
			}
		}

		return false;
	}

	// 看牌事件
	protected boolean OnUserKanPai(int wChairID) {
		if (wChairID >= 5) {
			System.out.printf("Error wChairID (%d)\n", wChairID);
			return false;
		}

		if(m_menQuan > 0)
		{
			if(m_cntGenZhu[wChairID] < m_menQuan)
			{
				return false;
			}
		}
		if(m_cbCardStatus[wChairID] == Constants.CS_KANPAI){
			//已经看过了
			return false;
		}
		m_wRobotSeconds[wChairID] = 0;

		m_bKanPai[wChairID] = true;
		m_cbCardStatus[wChairID] =  Constants.CS_KANPAI;

		SUBCMD_S_KanPai cmd = new SUBCMD_S_KanPai();
		cmd.m_lUid = m_pIUserItem[wChairID].dwUserID;
		cmd.m_wUserChairID = wChairID; // 玩家座位号
		cmd.wCurrentUser = (int) m_wCurrentUser; // 当前用户
		for (int i = 0; i < 3; i++) {
			cmd.bCardData[i] = m_bTableCardArray[wChairID][i]; // 用户扑克
		}
		m_GameLogic.SortCardList(m_bTableCardArray[wChairID], 3);

		SendTableData(Constants.INVALID_CHAIR, cmd.getJsonData());

		return false;
	}

	// 比牌事件
	protected boolean OnUserBiPai(int wChairID, int wDstChairID) {
		if (wChairID >= 5) {
			System.out.printf("Error wChairID (%d)\n", wChairID);
			return false;
		}
		int wPlayerCount = 0;
		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].cbUserStatus == US_PLAY)
				wPlayerCount++;
		}
		if (wPlayerCount < 2) {
			System.out.printf("[1]OnUserBiPai Error ChairID(%d)(%d)\n", m_wCurrentUser, wChairID);
			return false;
		}
		if (m_wCurrentUser != wChairID) {
			System.out.printf("[1]OnUserBiPai Error m_wCurrentUser != ChairID(%d)(%d)\n", m_wCurrentUser, wChairID);
			return false;
		}
		m_wRobotSeconds[wChairID] = 0;

		if (m_bKanPai[wChairID]) {
			m_lTableGold[wChairID] += m_lTurnBasicGold * 2;
			m_lTableGoldTotal += m_lTurnBasicGold * 2;
		} else {
			m_lTableGold[wChairID] += m_lTurnBasicGold;
			m_lTableGoldTotal += m_lTurnBasicGold;
		}

		SUBCMD_S_BiPai cmd = new SUBCMD_S_BiPai();
		cmd.m_srcUid = m_pIUserItem[wChairID].dwUserID; // 当前玩家
		cmd.m_dstUid = m_pIUserItem[wDstChairID].dwUserID; // 当前玩家
		cmd.m_srcSeat = wChairID;// 玩家座位号
		cmd.m_dstSeat = wDstChairID;// 玩家座位号

		if (m_GameLogic.CompareCard(m_bTableCardArray[wChairID], m_bTableCardArray[wDstChairID], 3) != 0) {
			cmd.m_loseUid = m_pIUserItem[wDstChairID].dwUserID; // 赢的用户
			cmd.m_loseSeat = wDstChairID; // 赢的座位号
			if (m_bKanPai[wDstChairID]) {
				m_cbCardStatus[wDstChairID] =  Constants.CS_MINGPAI_LOSE;
			} else {
				m_cbCardStatus[wDstChairID] =  Constants.CS_ANPAI_LOSE;
			}
			m_pIUserItem[wDstChairID].cbUserStatus = US_NULL;
		} else {
			cmd.m_loseUid = m_pIUserItem[wChairID].dwUserID; // 赢的用户
			cmd.m_loseSeat = wChairID; // 赢的座位号
			if (m_bKanPai[wChairID]) {
				m_cbCardStatus[wChairID] =  Constants.CS_MINGPAI_LOSE;
			} else {
				m_cbCardStatus[wChairID] =  Constants.CS_ANPAI_LOSE;
			}
			m_pIUserItem[wChairID].cbUserStatus = US_NULL;
		}

		cmd.wCurrentUser = (int) m_wCurrentUser; // 当前用户

		SendTableData(Constants.INVALID_CHAIR, cmd.getJsonData());

		m_wBiPaiDelay = 3;
		// OnUserJiaZhu(wChairID, 0L);

		return false;
	}

	// 开牌事件
	protected boolean OnUserKaiPai(int wChairID) {

		if (wChairID >= 5) {
			System.out.printf("Error wChairID (%d)\n", wChairID);
			return false;
		}

		int wPlayerCount = 0;
		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].cbUserStatus == US_PLAY)
				wPlayerCount++;
		}
		if (wPlayerCount != 2) {
			System.out.printf("[1]OnUserKaiPai Error ChairID(%d)(%d)\n", m_wCurrentUser, wChairID);
			return false;
		}
		/*
		 * if (m_wCurrentUser != wChairID) { System.out.printf(
		 * "[1]OnUserKaiPai Error m_wCurrentUser != ChairID(%d)(%d)\n",
		 * m_wCurrentUser, wChairID); return false; }
		 */

		m_wRobotSeconds[wChairID] = 0;

		if (m_bKanPai[wChairID]) {
			m_lTableGold[wChairID] += m_lTurnBasicGold * 2;
			m_lTableGoldTotal += m_lTurnBasicGold * 2;
		} else {
			m_lTableGold[wChairID] += m_lTurnBasicGold * 2;
			m_lTableGoldTotal += m_lTurnBasicGold * 2;
		}

		int wDstChairID = Constants.INVALID_CHAIR;
		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].dwUserID > 0 && m_pIUserItem[i].cbUserStatus == US_PLAY && i != wChairID) {
				wDstChairID = i;
			}
		}
		if (wDstChairID == Constants.INVALID_CHAIR) {
			if (m_bGameStarted) {
				OnEventGameEnd(Constants.INVALID_CHAIR, Constants.GER_NO_PLAYER);
			}
			System.out.printf("[2]OnUserKaiPai Error ChairID(%d)(%d)\n", m_wCurrentUser, wChairID);
			return false;
		}

		int wWinnerUser = DeduceWinner();
		if (wWinnerUser != wChairID && wWinnerUser != wDstChairID) {
			System.out.printf("[3]OnUserKaiPai Error ChairID(%d)(%d)\n", m_wCurrentUser, wChairID);
			return false;
		}

		m_bKanPai[wChairID] = true;
		m_bKanPai[wDstChairID] = true;

		SUBCMD_S_KaiPai cmd = new SUBCMD_S_KaiPai();
		cmd.m_srcUid = m_pIUserItem[wChairID].dwUserID; // 当前玩家
		cmd.m_dstUid = m_pIUserItem[wDstChairID].dwUserID; // 当前玩家
		cmd.m_winnerUid = m_pIUserItem[wWinnerUser].dwUserID; // 赢的用户
		cmd.m_srcSeat = wChairID;// 玩家座位号
		cmd.m_dstSeat = wDstChairID;// 玩家座位号
		cmd.m_winnerSeat = wWinnerUser; // 赢的座位号
		for (int i = 0; i < 3; i++) {
			cmd.srcCardData[i] = m_bTableCardArray[wChairID][i]; // 用户扑克
		}
		m_GameLogic.SortCardList(m_bTableCardArray[wChairID], 3);
		for (int i = 0; i < 3; i++) {
			cmd.dstCardData[i] = m_bTableCardArray[wDstChairID][i]; // 用户扑克
		}
		m_GameLogic.SortCardList(m_bTableCardArray[wDstChairID], 3);

		SendTableData(Constants.INVALID_CHAIR, cmd.getJsonData());

		if (m_bGameStarted) {
			OnEventGameEnd(wChairID, Constants.GER_NORMAL);
		}
		

		return false;
	}
	// 游戏函数

	// 推断胜者
	protected int DeduceWinner() {

		int k = 0;
		// 保存扑克
		int bTableCardArray[][] = m_bTableCardArray.clone();

		int bWinner = 0;
		// 寻找玩家
		for (k = 0; k < 5; k++) {
			if (m_pIUserItem[k].cbUserStatus == US_PLAY) {
				bWinner = k;
				break;
			}
		}

		// 对比玩家
		for (int i =  (k + 1); i < 5; i++) {
			if (m_pIUserItem[i].cbUserStatus == US_PLAY) {
				if (m_GameLogic.CompareCard(bTableCardArray[bWinner], bTableCardArray[i], 3) != 0) {
					start = true;
					Winner = bWinner;
					return bWinner;
				}

				else {
					bWinner = i;
					start = true;
					Winner = bWinner;
					return bWinner;
				}
			}
		}
		return bWinner;
	} // delete

	protected boolean ConcludeGame() {
		// 设置状态

		RepositTable();

		for (int i = 0; i < 5; ++i) {
			if (m_pIUserItem[i].dwUserID > 0) {
				m_pIUserItem[i].cbUserStatus = US_NULL;
			}
		}

		if (Winner == 0) {
			Winner =  m_wCurrentUser;
		}
		return true;
	}

	public int GetPlayerCount() {
		int cntPlayer = 0;
		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].dwUserID > 0) {
				cntPlayer++;
			}
		}
		return cntPlayer;
	}
	public int GetPlayerOnlineCount() {
		int cntPlayer = 0;
		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].dwUserID > 0 && m_pIUserItem[i].cbUserStatus == Constants.US_OFFLINE) {
				cntPlayer++;
			}
		}
		return cntPlayer;
	}
	public int GetRobotCount() {
		int cntPlayer = 0;
		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].isAndroidUser == 1) {
				cntPlayer++;
			}
		}
		return cntPlayer;
	}

	public int GetChipCount() {
		int cntChip = 0;
		for (int i = 0; i < 5; i++) {
			if (m_cntTableChip[i] > 0) {
				cntChip++;
			}
		}
		return cntChip;
	}

	public int GetPlayerCountByStatus(int cbUserStatus) {
		int cntPlayer = 0;
		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].cbUserStatus == cbUserStatus) {
				cntPlayer++;
			}
		}
		return cntPlayer;
	}
	public int GetRobotCountByStatus(int cbUserStatus) {
		int cntPlayer = 0;
		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].isAndroidUser == 2 && m_pIUserItem[i].cbUserStatus == cbUserStatus) {
				cntPlayer++;
			}
		}
		return cntPlayer;
	}
	public int CanSit(int _Uid) {
		if(this.isDwcType()){
			UserRedisInfo info = dthall.getInstance().getSafeUserRedisInfo(_Uid);
			if(info.json.optInt("userRmbType") == 1 && !canUserJoinRentRoom(info.json.optInt("upAgentUid"))){
				return 22;
			}else if(info.json.optInt("userRmbType") == 0 && m_clubOwnerId != 0){
				UserRedisInfo clubInfo = dthall.getInstance().getSafeUserRedisInfo(m_clubOwnerId);
				if(clubInfo.json.optInt("userRmbType") == 1){
					return 23;
				}
			}
		}
		if (m_locked != 0) {
			return 13;
		}
		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].dwUserID == _Uid) {
				SUBCMD_S_OnLine cmd = new SUBCMD_S_OnLine();
				cmd.uid = _Uid;
				cmd.seat = i;
				SendTableData(Constants.INVALID_CHAIR, cmd.getJsonData());
				return i;
			}
		}
		SHMUser user = dthall.getInstance().getUserInfo(_Uid);
		if (user != null && isZuJuType()) {
			if (m_payWay.equalsIgnoreCase("CLUBPAY") || m_payWay.equalsIgnoreCase("WINPAY")) {
				if(m_payWay.equalsIgnoreCase("CLUBPAY")){
					for (int i = 0; i < 5; i++) {
						if (m_pIUserItem[i].dwUserID == 0) {
							return i;
						}
					}
					return 12;
				}
				if (user.getGem(0) < m_byCostGem * 5) {
					return 11;
				}
			}else{
				if (m_payWay.equalsIgnoreCase("AAPAY") )
				{
					if (user.getGem(0) < m_byCostGem) {
						return 11;
					}
				}
			}
		}
		if(user != null && isDwcType()){
			if(user.getRmb1v1Value() < m_ruchang){
				return 21;
			}
		}
		if(user != null && isGoldType()){
			if(user.getCoin(0) < m_ruchang){
				return 21;
			}
		}
		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].dwUserID == _Uid) {
				if (m_pIUserItem[i].dwUserID == _Uid) {
					SUBCMD_S_OnLine cmd = new SUBCMD_S_OnLine();
					cmd.uid = _Uid;
					cmd.seat = i;
					SendTableData(Constants.INVALID_CHAIR, cmd.getJsonData());
				}
				return i;
			}
		}
		if (m_locked != 0) {
			return 13;//已经开始 并且房主不让近
		}	
		
		int cntUser = 0;
		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].dwUserID > 0) {
				cntUser++;
			}
		}
		if (cntUser >= 5) {
			return 13;
		}
		
		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].dwUserID == 0) {
				return i;
			}
		}

		return 12;
	};

	public void SendAvatar(int _Uid) {
		int wChairID = FindSeat(_Uid);
		if (wChairID == Constants.INVALID_CHAIR) {
			return;
		}
		String iter = dthall.getInstance().Handle_OnGetUserAvatar(_Uid);

		SUBCMD_S_Avatar cmd = new SUBCMD_S_Avatar();
		cmd.m_lUid = _Uid;
		if (!StringUtils.isEmpty(iter)) {
			cmd.szAvatarURL = iter;
		} else {
			cmd.szAvatarURL = PublishDef.getDefaultHeadAvatar();
		}

		SendTableData(Constants.INVALID_CHAIR, cmd.getJsonData());
	}

	public  long currDealTime = 0;
	void OnDealAI(int dt) {
		m_DealTime = (int) (System.currentTimeMillis() / 1000);
		if(currDealTime == 0){
			currDealTime = System.currentTimeMillis();
		}else{
			long time = System.currentTimeMillis();
			if(time - currDealTime >= 1000){
				currDealTime = time;
			}else{
				return;
			}
		}
		if (m_wFaPaiDelay > 0) {
			m_wFaPaiDelay--;
			return;
		}
//		if(this.isZuJuType()){
//			return;
//		}
		int bTableCardArray[][] = new int[5][3];
		for(int i=0;i<5;i++)
		{
			for(int j=0;j<3;j++)
			{
				bTableCardArray[i][j] = m_bTableCardArray[i][j];
			}
		}
		
		int cntPlay = GetPlayerCountByStatus(US_PLAY);
		int cntRobotPlay = GetRobotCountByStatus(US_PLAY);
		int duXing[] = {800,3000,4500,6000,8000,9800,9900};
		
		m_wRobotSeconds[m_wCurrentUser]++;
		if(m_wCurrentUser < 6)
		{
			if(m_pIUserItem[m_wCurrentUser].isAndroidUser == 2)
			{
				if (m_chipRound >= 12 + CommonDef.getRandomInt(5)) {
					if (m_wBiPaiDelay > 0) {
						m_wBiPaiDelay--;
						return;
					}
					if(cntPlay == 2)
					{
						if(CommonDef.getRandomInt(100) > 20){
							OnUserKaiPai(m_wCurrentUser);
						}else{
							OnUserGenZhu(m_wCurrentUser);
						}
						return;
					}else{
						for(int wDstChairID = 1 ;wDstChairID < 5;wDstChairID++)
						{
							if(m_pIUserItem[wDstChairID].isAndroidUser == 0 && m_pIUserItem[wDstChairID].cbUserStatus == US_PLAY)
							{
								OnUserBiPai(m_wCurrentUser,wDstChairID);								
								return;
							}
						}								
					}
				}
				if(this.isGoldType()){
					if(m_pIUserItem[m_wCurrentUser].lUserCoin < m_lichang * 0.5){
						if(cntPlay == 2)
						{
							if(CommonDef.getRandomInt(100) > 20){
								OnUserKaiPai(m_wCurrentUser);
							}else{
								OnUserGenZhu(m_wCurrentUser);
							}
						}else{
							OnUserFangQi(m_wCurrentUser);
						}
						return;
					}
				}else if(this.isDwcType()){
					if(m_pIUserItem[m_wCurrentUser].lUserRmb < m_lichang * 0.5){
						if(cntPlay == 2)
						{
							if(CommonDef.getRandomInt(100) > 20){
								OnUserKaiPai(m_wCurrentUser);
							}else{
								OnUserGenZhu(m_wCurrentUser);
							}
						}else{
							OnUserFangQi(m_wCurrentUser);
						}
						return;
					}
				}
				if(m_lTableGold[m_wCurrentUser] >= m_lRoundLimit){
					if(cntPlay == 2)
					{
						if(CommonDef.getRandomInt(100) > 20){
							OnUserKaiPai(m_wCurrentUser);
						}else{
							OnUserGenZhu(m_wCurrentUser);
						}
					}else{
						OnUserFangQi(m_wCurrentUser);
					}
					return;
				}
				int card_type = m_GameLogic.GetCardType(m_bTableCardArray[m_wCurrentUser],3);
				if(card_type > 7 || card_type < 0)
				{
					card_type = 1;
				}
				int delay_rate = 2000 +m_wRobotYongQi[m_wCurrentUser];
				if(delay_rate > 8000)
				{
					delay_rate = 8000;
				}
				if(delay_rate < 5000)
				{
					delay_rate = 5000;
				}
				if(m_wRobotSeconds[m_wCurrentUser] > 1 && (Constants.rand(1, 9000) < delay_rate))
				{
					if(m_bKanPai[m_wCurrentUser] == false && Constants.rand(1, 10000) < 4000)
					{
						OnUserKanPai(m_wCurrentUser);
						return;
					}		
					m_wRobotSeconds[m_wCurrentUser] = 0;
					
					if ((cntPlay == 2 || cntRobotPlay == cntPlay) && Constants.rand(1, 10000) < 1500)
					{
						boolean bFangQi = false;
						for(int wDstChairID = 1 ;wDstChairID < 5;wDstChairID++)
						{
							if(wDstChairID != m_wCurrentUser && m_pIUserItem[wDstChairID].cbUserStatus == US_PLAY 
							&& m_GameLogic.CompareCard(bTableCardArray[wDstChairID],bTableCardArray[m_wCurrentUser], 3) == 1)
							{
								bFangQi = true;
								OnUserKaiPai(m_wCurrentUser);	
								return;              
							}
						}
						if(GetChipCount() > 20 + Constants.rand(1, 10))
						{
							OnUserKaiPai(m_wCurrentUser);	
						}else
						{
							OnUserGenZhu(m_wCurrentUser);
						}
						return;
					}else  if(m_lTurnBasicGold < m_lCellScore * 3 && Constants.rand(1, 10000) < 1000)
					{
						OnUserJiaZhu(m_wCurrentUser,3 * m_lCellScore);
					}else if(m_lTurnBasicGold < m_lCellScore * 5 && Constants.rand(1, 10000) < 2000)
					{
						OnUserJiaZhu(m_wCurrentUser,5 * m_lCellScore);
					}else if(m_bKanPai[m_wCurrentUser] == true)
					{			
						boolean bFangQi = false;
						for(int wDstChairID = 0 ;wDstChairID < 5;wDstChairID++)
						{
							if(wDstChairID != m_wCurrentUser && m_pIUserItem[wDstChairID].cbUserStatus == US_PLAY 
							&& m_GameLogic.CompareCard(bTableCardArray[wDstChairID],bTableCardArray[m_wCurrentUser], 3) == 1)
							{
								bFangQi = true;
								OnUserFangQi(m_wCurrentUser);	
								return;              
							}
							if(CommonDef.getRandomInt(100) > 90 && cntPlay == 2 && m_pIUserItem[wDstChairID].isAndroidUser == 2 && m_pIUserItem[m_wCurrentUser].isAndroidUser == 2){
								bFangQi = true;
								OnUserFangQi(m_wCurrentUser);	
								return; 
							}
						}
						if(bFangQi == false)
						{
							OnUserGenZhu(m_wCurrentUser);
							return;
						}
					}else  
					{
						if(m_lTableGold[m_wCurrentUser]  > m_lTurnBasicGold * 5)
						{
							if(m_bKanPai[m_wCurrentUser] == true && card_type < Constants.CT_SHUN_ZI)
							{
								if(Constants.rand(1, 10000) > duXing[card_type-1] + m_wRobotYongQi[m_wCurrentUser])
								{
									OnUserFangQi(m_wCurrentUser);
								}else{
									OnUserGenZhu(m_wCurrentUser);
								}
								return;
							}else{
								if(cntPlay > 2)
								{
									for(int wDstChairID = 1 ;wDstChairID < 5;wDstChairID++)
									{
										if(m_pIUserItem[wDstChairID].isAndroidUser == 0 && m_pIUserItem[wDstChairID].cbUserStatus == US_PLAY)
										{
											OnUserBiPai(m_wCurrentUser,wDstChairID);
											return;
										}
									}								
								}
								for(int wDstChairID = 1 ;wDstChairID < 5;wDstChairID++)
								{
									if(m_pIUserItem[wDstChairID].isAndroidUser == 0 && m_pIUserItem[wDstChairID].cbUserStatus == US_PLAY)
									{
										if(m_bKanPai[m_wCurrentUser] == true && m_GameLogic.CompareCard(bTableCardArray[wDstChairID],bTableCardArray[m_wCurrentUser], 3) == 1)
										{
											OnUserFangQi(m_wCurrentUser);
											return;
										}
									}
								}									
								
								OnUserGenZhu(m_wCurrentUser);
								return;
							}					
						}else{
							OnUserGenZhu(m_wCurrentUser);
							return;
						}
					}
								
				}		
			}else{
				if(m_wBiPaiDelay > 0)
				{
					m_wBiPaiDelay--;
					if(m_wBiPaiDelay == 0)
					{
						OnUserJiaZhu(m_wCurrentUser,0);
					}
				}else{
					if(m_wRobotSeconds[m_wCurrentUser]> 60)
					{
						OnUserFangQi(m_wCurrentUser);
					}
				}
			}

		}	
	}

	public int m_DealTime = 0;
	public int getDealTime()
	{
		return m_DealTime;
	}
	public long m_handlePrevTime = 0;
	public void OnDeal(int dt) {
		try {
			Long tnow = System.currentTimeMillis() / 1000;
			if (m_lStartTime == 0L) {
				m_lStartTime = tnow;
			}			
			if(m_handlePrevTime == 0){
				m_handlePrevTime = System.currentTimeMillis();
			}
			Long currnow = System.currentTimeMillis();
			if(currnow - m_handlePrevTime < 1000){
				return;
			}else{
				m_handlePrevTime = currnow;
			}

			int cntOnline = 0;
			int cntOffline = 0;
			for (int i = 0; i < 5; i++) {
				if (m_pIUserItem[i].dwUserID > 0 && m_pIUserItem[i].isAndroidUser == 0) {
					if (m_pIUserItem[i].cbOnline == Constants.US_OFFLINE) {
						cntOffline++;
					} else {
						cntOnline++;
					}
				}
			}
			if (m_dismissTimer > 0 && tnow - m_dismissTimer > 60) {

				OnEventGameEnd(0, GAME_END_DISMISS);

				m_dismissTimer = (long) 0;

			} else if (m_bGameStarted == true) {
				OnDealAI(dt);

			} else {
				addRobotUser();
				int cntReady = 0;
				if (m_wFaPaiDelay > 0) {
					m_wFaPaiDelay--;
					return;
				}
				for (int wChairID = 0; wChairID < 5; wChairID++) {
					if(m_pIUserItem[wChairID].isAndroidUser == 2){
						if(this.isGoldType()){
							if(m_pIUserItem[wChairID].cbUserStatus != US_READY){
								if(m_pIUserItem[wChairID].lUserCoin < m_lichang){//做一个delay，更真实
									if(this.m_autoGameIapDelay > 0){
										this.m_autoGameIapDelay--;
										return;
									}else if(this.m_autoGameIapDelay == 0){
										this.m_autoGameIapDelay = 10;
									}
									int [] applylist = {18888 + 2188,18888 + 2188,28888 + 2188,28888 + 2188,188888 + 2188,288888 + 2188,m_lichang + 1888};
									int applynum = 0;
									while(applynum < m_lichang){
										applynum = applylist[(int) (Math.random() * applylist.length)];
									}
									int gold = GoldMgr.getInstance().applyRobotGold(applynum);
									if(gold == 0.0){
										boolean hasdissmisstry = false;
										for(int k = 0;k < 5;k++){
											if(k == wChairID){
												continue;
											}
											if(m_byAgreeDismiss[k] == 1){
												hasdissmisstry = true;
											}
										}
										if(!hasdissmisstry && m_byAgreeDismiss[wChairID] != 1){
											String vecArg[] = {"normaluser","" + m_pIUserItem[wChairID].dwUserID};
											Handle_onTryDismissRoom(m_pIUserItem[wChairID].dwUserID, vecArg);
										}
										return;
									}else{
										SHMUser user = dthall.getInstance().getUserInfo(m_pIUserItem[wChairID].dwUserID);
										m_pIUserItem[wChairID].lUserCoin += gold;
										user.addApplyNum(gold);
										user.getCoin(gold);
										Handle_GameIap(m_pIUserItem[wChairID].dwUserID, new String[2]);
									}
								}else{
									if(CommonDef.getRandomInt(100) < 60 && m_pIUserItem[wChairID].cbUserStatus != US_READY){
										Handle_onReady(m_pIUserItem[wChairID].dwUserID, new String[2]);
									}
								}
							}
						}else if(this.isDwcType()){
							if(m_pIUserItem[wChairID].cbUserStatus != US_READY){
								if(m_pIUserItem[wChairID].lUserRmb < m_lichang){//做一个delay，更真实
									if(this.m_autoGameIapDelay > 0){
										this.m_autoGameIapDelay--;
										return;
									}else if(this.m_autoGameIapDelay == 0){
										this.m_autoGameIapDelay = 10;
									}
									double [] applylist = {49.0,49.0,49.0,199.0,100.0,499.0,1299.0,1000.0,2000.0,m_lichang + 99.0};
									double applynum = 0.0;
									while(applynum < m_doubleDiFen){
										applynum = applylist[(int) (Math.random() * applylist.length)];
									}
									double rmb = GoldMgr.getInstance().applyRobotRmb(applynum);
									if(rmb == 0.0){
										boolean hasdissmisstry = false;
										for(int k = 0;k < 5;k++){
											if(k == wChairID){
												continue;
											}
											if(m_byAgreeDismiss[k] == 1){
												hasdissmisstry = true;
											}
										}
										if(!hasdissmisstry && m_byAgreeDismiss[wChairID] != 1){
											String vecArg[] = {"normaluser","" + m_pIUserItem[wChairID].dwUserID};
											Handle_onTryDismissRoom(m_pIUserItem[wChairID].dwUserID, vecArg);
										}
										return;
									}else{
										SHMUser user = dthall.getInstance().getUserInfo(m_pIUserItem[wChairID].dwUserID);
										m_pIUserItem[wChairID].lUserRmb += rmb;
										user.addApplyNum(rmb);
										user.addRmb1v1Value(rmb);
										Handle_GameIap(m_pIUserItem[wChairID].dwUserID, new String[2]);
									}
								}else{
									if(CommonDef.getRandomInt(100) < 60 && m_pIUserItem[wChairID].cbUserStatus != US_READY){
										Handle_onReady(m_pIUserItem[wChairID].dwUserID, new String[2]);
									}
								}
							}
						}
					}
				}
				for (int wChairID = 0; wChairID < 5; wChairID++) {
					if (m_pIUserItem[wChairID].dwUserID > 0 && m_pIUserItem[wChairID].cbUserStatus == US_READY) {
						cntReady++;
					}
					if (m_wRobotSeconds[wChairID] > 0) {
						m_wRobotSeconds[wChairID]--;
					}
				}

				if (cntReady >= 2) {
					if (m_byRound <= 1) {
						if (m_pIUserItem[0].cbUserStatus == US_READY) {
							needDealTime--;
							if (cntReady == cntOnline || cntReady == GetPlayerCount() || needDealTime == 0) {
								needDealTime = 30;
								OnEventGameStart();
							}
						}
					}else {
						needDealTime--;
						if (cntReady == cntOnline || cntReady == GetPlayerCount() || needDealTime == 0) {
							needDealTime = 30;
							OnEventGameStart();
						}
					}

				} else {
					needDealTime = 15;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	void HuanPai() {
		if (random(0, 10000) >= GameConfig.AnKongPoint) {
			return;
		}
		int needSit = -1;
		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].dwUserID > 0) {
				if (GameConfig.IsAnKongUid(m_pIUserItem[i].dwUserID)) {
					needSit = i;
					break;// TODO第一个需要暗控的人
				}
			}
		}
		if (needSit == -1) {
			return;
		}
		int maxSit = 0;
		for (int i = 1; i < 5; i++) {
			if (m_pIUserItem[i].dwUserID > 0) {
				if (m_GameLogic.CompareCard(m_bTableCardArray[maxSit],
						m_bTableCardArray[i], 3) != 0) {
				} else {
					maxSit = i;
				}
			}
		}
		if (needSit != -1 && maxSit != -1 && needSit != maxSit) {
			for (int i = 0; i < 3; i++) {
				int temp = m_bTableCardArray[maxSit][i];
				m_bTableCardArray[maxSit][i] = m_bTableCardArray[needSit][i];
				m_bTableCardArray[needSit][i] = temp;
			}
		}
	}
	// 游戏开始
	public void FaPai() {

		m_bTableStarted = true;
		m_bGameStarted = true;
		
		m_chipRound = 0;
		
		m_menQuan = m_staticmenQuan;
		if (Winner == 0 && m_wCurrentUser < 5) {
			Winner = m_wCurrentUser;
		}
		if (m_wCurrentUser == 0 || m_wCurrentUser == Constants.INVALID_CHAIR) {
			m_wCurrentUser = 0;
		}

		m_wFaPaiDelay = 5;
		// 状态变量

		m_bSendCardCount = 0;
		for (int i = 0; i < 5; i++) {
			m_bKanPai[i] = false;
			m_bTableCardCount[i] = 0;
			m_lTableGold[i] = 0;
			m_cntGenZhu[i] = 0;
		}

		// 金币变量
		m_lTurnBasicGold = m_lCellScore;

		if(m_lTurnBasicGold < m_diFen)
		{
			m_lTurnBasicGold = m_diFen;
		}
		// 分发扑克
		m_bSendCardCount = 3; // been 2

		for (int loop = 0; loop < 2; loop++) {

			int cardArray[] = new int[5 * 3];
			m_GameLogic.RandCardList(cardArray, 5 * 3);
			int x = 0;
			int y = 0;
			for(int i = 0;i<5*3;i++){
				m_bTableCardArray[x][y] = cardArray[i];
				y++;
				if(y == 3){
					x++;
					y=0;
				}
			}
			
			
			int nSingleCard = 0;
			for (int i = 0; i < 5; i++) {
				if (m_pIUserItem[i].dwUserID > 0 && m_pIUserItem[i].cbUserStatus == US_READY) {
					if (m_GameLogic.GetCardType(m_bTableCardArray[i], 3) == Constants.CT_SINGLE) {
						/*
						int rand = random(0, 10000);
						if (rand < 300) {
							m_GameLogic.GetDaPai(m_bTableCardArray[i], Constants.CT_JIN_HUA);
						} else if (rand < 800) {
							m_GameLogic.GetDaPai(m_bTableCardArray[i], Constants.CT_SHUN_ZI);
						} else if (rand < 3000) {
							m_GameLogic.GetDaPai(m_bTableCardArray[i], Constants.CT_DOUBLE);
						}
						*/
					}
					if (m_GameLogic.GetCardType(m_bTableCardArray[i], 3) == Constants.CT_SINGLE) {
						nSingleCard++;
					}
				} else {
					if (m_pIUserItem[i].dwUserID > 0 && m_pIUserItem[i].cbUserStatus != US_READY) {
						m_cntNotReady[i]++;
					}

				}
			}

			if (nSingleCard <= 2) {

				break;
			}
			
		}
		//群控操作
		boolean hasrobot = false;
		ArrayList<Integer> vecEmpty = new ArrayList<>();
		for (int i = 0; i < 5; i++) {
			if(m_pIUserItem[i].isAndroidUser == 2){
				hasrobot = true;
				vecEmpty.add(i);
			}
		}
		int changeRobotIndex = -1;
		double rate = 3.0;
		if(this.isDwcType() && hasrobot && m_doubleDiFen >= GoldMgr.getInstance().getRobotRmbDiFenControl() 
				&& CommonDef.getRandomInt(100) < GoldMgr.getInstance().getRobotRmbDiFenControlRate_KaiPai()){
			//真金模式，有机器人么，底分超过控制，开始强制换牌
			changeRobotIndex = vecEmpty.get(CommonDef.randInt(0,vecEmpty.size()));
		}

		if(changeRobotIndex == -1 && hasrobot && GoldMgr.getInstance().getQunKongRev("ZJH",m_doubleDiFen) > 0){
			changeRobotIndex = vecEmpty.get(CommonDef.randInt(0,vecEmpty.size()));
		}
		if((this.isGoldType() || this.isDwcType() || this.isZuJuType()) && (changeRobotIndex > 0 || CommonDef.getRandomInt(100) < m_qunKongRate * 100)){			
			double wonRate[] = new double[5];
			boolean needAdjust = false;
			for(int i = 0;i < 5;i++){
				SHMUser user = dthall.getInstance().getUserInfo(m_pIUserItem[i].dwUserID);
				if(user != null && m_pIUserItem[i].cbUserStatus == US_READY){
					if(this.isGoldType())
						wonRate[i] = user.getUserGoldWonRate();
					else if(this.isDwcType()){
						wonRate[i] = user.getUserRmbWonRate();
						if(i == changeRobotIndex){
							wonRate[i] = rate;
						}
					}else if(this.isZuJuType())
						wonRate[i] = user.getUserZuJuWonRate();
					if(wonRate[i] <= m_qunKongMinTh || wonRate[i] >= m_qunKongMaxTh)
						needAdjust = true;
				}else{
					wonRate[i] = -10000;
				}
			}
			if(needAdjust){
				//需要群控处理,先对牌进行打分,在替换牌
				int paiScore[] = new int[5];
				int origPai[][] = new int[5][3];
				for(int i = 0;i < 5;i++){
					if( m_pIUserItem[i].cbUserStatus == US_READY){
						paiScore[i] = m_GameLogic.GetCardType(m_bTableCardArray[i], 3);
						Constants.memcpy(origPai[i], m_bTableCardArray[i], 3);
					}else{
						paiScore[i] = -100000;
					}
				}
				//换牌
				for(int i = 0;i < 5;i++){
					if(paiScore[i] <= -100000)
						continue;
					int rateIndex = getRankIndex(wonRate,-100.0);
					wonRate[rateIndex] = -10000;
					int scoreIndex = getRankIndex(paiScore,-100);
					paiScore[scoreIndex] = -10000;
					Constants.memcpy(m_bTableCardArray[rateIndex], origPai[scoreIndex],3);
				}
			}
		}else{
			if(hasAutoDianKong() && CommonDef.getRandomInt(100) < GoldMgr.getInstance().getDianKongValidRate()){
				double wonRate[] = new double[5];
				for(int i = 0;i < 5;i++){
					if(m_pIUserItem[i].dwUserID > 0){
						wonRate[i] = getDianKongRate(m_pIUserItem[i].dwUserID);
					}else{
						wonRate[i] = -10000;
					}
				}
				//需要群控处理,先对牌进行打分,在替换牌
				int paiScore[] = new int[5];
				int origPai[][] = new int[5][3];
				for(int i = 0;i < 5;i++){
					if( m_pIUserItem[i].cbUserStatus == US_READY){
						paiScore[i] = m_GameLogic.GetCardType(m_bTableCardArray[i], 3);
						Constants.memcpy(origPai[i], m_bTableCardArray[i], 3);
					}else{
						paiScore[i] = -100000;
					}
				}
				//换牌
				for(int i = 0;i < 5;i++){
					if(paiScore[i] <= -100000)
						continue;
					int rateIndex = getRankIndex(wonRate,-100.0);
					wonRate[rateIndex] = -10000;
					int scoreIndex = getRankIndex(paiScore,-100);
					paiScore[scoreIndex] = -10000;
					Constants.memcpy(m_bTableCardArray[rateIndex], origPai[scoreIndex],3);
				}
			}
		}
		// //计算数组的个数

		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].dwUserID > 0 && m_pIUserItem[i].cbUserStatus == US_READY) {
				m_bTableCardCount[i] = m_bSendCardCount;

				m_pIUserItem[i].cbUserStatus = US_PLAY;

				m_lTableGold[i] = m_lTurnBasicGold;
				m_lTableGoldTotal += m_lTurnBasicGold;
				if(m_diFen <= 1)
				{
					m_cntTableChip[0] = 1;	
				}else if(m_diFen <= 3)
				{
					m_cntTableChip[2] = 1;
				}else if(m_diFen <= 5)
				{
					m_cntTableChip[4] = 1;
				}				
			} else {
				m_pIUserItem[i].cbUserStatus = US_NULL;
			}
		}

		// 获取当前玩家
		if (m_wCurrentUser == Constants.INVALID_CHAIR) {
			for (int i = 0; i < 5; i++) {
				if (m_pIUserItem[i].cbUserStatus == US_READY) {
					m_wCurrentUser = i;
					break;
				}
			}
		} else {
			for (int i = 1; i < 6; i++) {
				m_wCurrentUser = (Winner + i) % 5;
				System.out.printf("[1.2] m_wCurrentUser (%d)(%d)(%d)\n", i, m_wCurrentUser, Winner);
				if (m_pIUserItem[m_wCurrentUser].cbUserStatus == US_PLAY)
					break;
			}
		}
		System.out.printf("[2] m_wCurrentUser (%d)\n", m_wCurrentUser);

		SUBCMD_S_FaPai cmd = new SUBCMD_S_FaPai();

		cmd.byRound = m_byRound;
		cmd.wCurrentUser = (int) m_wCurrentUser; // 当前玩家
		cmd.lTurnMaxGold = 1000000000; // 最大下注
		cmd.lTurnMinGold = m_diFen; // 最少下注
		cmd.m_lCellScore = m_lCellScore;
		// cmd.vecIsReady[5]; //就绪的用户扑克

		for (int i = 0; i < 5; ++i) {
			if (m_pIUserItem[i].cbUserStatus == US_PLAY) {
				cmd.vecIsReady[i] = 1;
				if(m_diFen == 5)
				{
					m_cntTableChip[4]++;
				}else if(m_diFen == 3)
				{
					m_cntTableChip[2]++;
				}else{
					m_cntTableChip[0]++;	
				}
				
				m_cbCardStatus[i] =  Constants.CS_ANPAI;
			} else {
				cmd.vecIsReady[i] = 0;
			}
		}

		// String sData = cmd.getJsonData();
		// dthall.getInstance().onSend(_Uid, sData);

		SendTableData(Constants.INVALID_CHAIR, cmd.getJsonData());
	}

	int getRankIndex(double[] score,double limit){
		int index = 0;
		for(int i = 0;i < score.length;i++){
			if(score[i] >= -10000 && score[i] >= limit){
				limit = score[i];
				index = i;
			}
		}
		return index;
	}
	int getRankIndex(int[] score,int limit){
		int index = 0;
		for(int i = 0;i < score.length;i++){
			if(score[i] >= -10000 && score[i] >= limit){
				limit = score[i];
				index = i;
			}
		}
		return index;
	}
	public void Handle_onJoin(GameAgent agent) {

	}

	public void Handle_onReady(int _Uid, String[] vecArg) {
		int wChairID = FindSeat(_Uid);
		if (wChairID == Constants.INVALID_CHAIR) {
			System.out.printf("Handle_onKanPai Error ChairID(%d)(%d)\n", m_wCurrentUser, wChairID);
			return;
		}

		m_wRobotSeconds[wChairID] = 0;
		OnUserReady(wChairID);

	}

	public void Handle_onRobot(int _Uid, String[] vecArg) {
		if (m_bGameStarted == true) {
			return;
		}
		Vector<String> tmpArg;
		int bySeatID = FindSeat(_Uid);
		if (bySeatID < 5) {
			m_pIUserItem[bySeatID].cbUserStatus = US_READY;
		}
		int cntPlayer = 0;
		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].dwUserID > 0) {
				cntPlayer++;
			}
		}
		if (cntPlayer < 5) {
			for (int i = 0; i < 5 - cntPlayer; i++) {
				// AddRobot();
			}
		}
	}

	// 放弃
	public void Handle_onFangQi(int _Uid, String[] vecArg) {
		if (m_bGameStarted == false) {
			return;
		}
		int wChairID = FindSeat(_Uid);
		if (wChairID == Constants.INVALID_CHAIR) {
			System.out.printf("Handle_onKanPai Error ChairID(%d)(%d)\n", m_wCurrentUser, wChairID);
			return;
		}
		m_wRobotSeconds[wChairID] = 0;
		OnUserFangQi(wChairID);
	}

	// 跟注
	public void Handle_onGenZhu(int _Uid, String[] vecArg) {
		if (m_bGameStarted == false) {
			return;
		}
		int wChairID = FindSeat(_Uid);
		if (wChairID == Constants.INVALID_CHAIR || m_wCurrentUser != wChairID) {
			System.out.printf("Handle_onJiaZhu Error ChairID(%d)(%d)\n", m_wCurrentUser, wChairID);
			return;
		}
		m_wRobotSeconds[wChairID] = 0;
		OnUserGenZhu(wChairID);
	}

	// 加注
	public void Handle_onJiaZhu(int _Uid, String[] vecArg) {
		if (m_bGameStarted == false) {
			return;
		}
		int wChairID = FindSeat(_Uid);
		if (wChairID == Constants.INVALID_CHAIR || m_wCurrentUser != wChairID) {
			System.out.printf("Handle_onJiaZhu Error ChairID(%d)(%d)\n", m_wCurrentUser, wChairID);
			return;
		}
		if (vecArg.length < 2)
			return;
		int lGold = Integer.parseInt(vecArg[1]);
		if (lGold < m_lTurnBasicGold) {
			System.out.printf("INVALID ADDGOLD[%d] \n", lGold);
			return;
		}
		if (lGold > 0) {
			m_wRobotSeconds[wChairID] = 0;
			OnUserJiaZhu(wChairID, lGold);
		}
	}

	// 看牌
	public void Handle_onKanPai(int _Uid, String[] vecArg) {
		if (m_bGameStarted == false) {
			return;
		}
		int wChairID = FindSeat(_Uid);
		if (wChairID == Constants.INVALID_CHAIR) {
			System.out.printf("Handle_onKanPai Error ChairID(%d)(%d)\n", m_wCurrentUser, wChairID);
			return;
		}
		m_wRobotSeconds[wChairID] = 0;
		OnUserKanPai(wChairID);
	}

	// 比牌
	public void Handle_onBiPai(int _Uid, String[] vecArg) {
		if (m_bGameStarted == false) {
			return;
		}
		int wChairID = FindSeat(_Uid);
		if (wChairID == Constants.INVALID_CHAIR || m_wCurrentUser != wChairID) {
			System.out.printf("Handle_onBiPai Error ChairID(%d)(%d)\n", m_wCurrentUser, wChairID);
			return;
		}

		if (vecArg.length < 2)
			return;

		int wDstChairID = Integer.parseInt(vecArg[1]);
		m_wRobotSeconds[wChairID] = 0;
		if (GetPlayerCountByStatus(US_PLAY) > 2) {
			OnUserBiPai(wChairID, wDstChairID);
		} else if (GetPlayerCountByStatus(US_PLAY) == 2) {
			OnUserKaiPai(wChairID);
		} else {
			if (m_bGameStarted) {
				OnEventGameEnd(wChairID, Constants.GER_NO_PLAYER);
			}			
		}

	}

	// 比牌
	public void Handle_onKaiPai(int _Uid, String[] vecArg) {
		if (m_bGameStarted == false) {
			return;
		}
		int wChairID = FindSeat(_Uid);
		if (wChairID == Constants.INVALID_CHAIR || m_wCurrentUser != wChairID) {
			System.out.printf("Handle_onKaiPai Error ChairID(%d)(%d)\n", m_wCurrentUser, wChairID);
			return;
		}
		m_wRobotSeconds[wChairID] = 0;
		OnUserKaiPai(wChairID);
	}

	// 表情
	public void Handle_onBiaoQing(int _Uid, String[] vecArg) {		
		int wChairID = FindSeat(_Uid);
		if (wChairID == Constants.INVALID_CHAIR) {
			System.out.printf("Handle_onBiaoQing Error ChairID(%d)(%d)\n", m_wCurrentUser, wChairID);
			return;
		}
		if (vecArg.length < 2)
			return;
		int index = Integer.parseInt(vecArg[1]);

		SUBCMD_S_BiaoQing cmd = new SUBCMD_S_BiaoQing();
		cmd.m_lUid = _Uid;
		cmd.m_wUserChairID = wChairID;
		cmd.m_iBiaoQingIndex = index;

		SendTableData(Constants.INVALID_CHAIR, cmd.getJsonData());
		if((this.isGoldType() || this.isDwcType()) && m_pIUserItem[wChairID].isAndroidUser == 0){
			for(int i = 0;i < 5;i++){
				if(m_pIUserItem[i].isAndroidUser == 2){
					if(CommonDef.getRandomInt(100) < 30){
						class SendBiaoQingTask extends TimerTask{
							int index = 0;
							int seat = 0;
							int uid = 0;
							@Override
							public void run() {
								// TODO Auto-generated method stub
								SUBCMD_S_BiaoQing cmd = new SUBCMD_S_BiaoQing();
								cmd.m_lUid = uid;
								cmd.m_wUserChairID = seat;
								cmd.m_iBiaoQingIndex = index;
								SendTableData(Constants.INVALID_CHAIR, cmd.getJsonData());
							}
						}
						Timer timer = new Timer();
						SendBiaoQingTask task = new SendBiaoQingTask();
						task.index = 1 + CommonDef.getRandomInt(11);
						task.seat = i;
						task.uid = m_pIUserItem[i].dwUserID;
						timer.schedule(task,1000 + CommonDef.getRandomInt(4000));
						return;
					}
				}
			}
		}
	}

	// 语音
	public void Handle_onYuYin(int _Uid, String[] vecArg) {
		int wChairID = FindSeat(_Uid);
		if (wChairID == Constants.INVALID_CHAIR) {
			System.out.printf("Handle_onYuYin Error ChairID(%d)(%d)\n", m_wCurrentUser, wChairID);
			return;
		}
		if (vecArg.length < 2)
			return;
		int index = Constants.atoi(vecArg[1]);;
		try {
			int a = Integer.parseInt(vecArg[1]);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}

		SUBCMD_S_YuYin cmd = new SUBCMD_S_YuYin();
		cmd.m_lUid = _Uid;
		cmd.m_wUserChairID = wChairID;
		cmd.m_iYuYinIndex = index;

		SendTableData(Constants.INVALID_CHAIR, cmd.getJsonData());
		if((this.isGoldType() || this.isDwcType()) && m_pIUserItem[wChairID].isAndroidUser == 0){
			for(int i = 0;i < 5;i++){
				if(m_pIUserItem[i].isAndroidUser == 2){
					if(CommonDef.getRandomInt(100) < 30){
						class SendBiaoQingTask extends TimerTask{
							int index = 0;
							int seat = 0;
							int uid = 0;
							@Override
							public void run() {
								// TODO Auto-generated method stub
								SUBCMD_S_YuYin cmd = new SUBCMD_S_YuYin();
								cmd.m_lUid = uid;
								cmd.m_wUserChairID = seat;
								cmd.m_iYuYinIndex = index;
								SendTableData(Constants.INVALID_CHAIR, cmd.getJsonData());
							}
						}
						Timer timer = new Timer();
						SendBiaoQingTask task = new SendBiaoQingTask();
						task.index = 0 + CommonDef.getRandomInt(5);
						task.seat = i;
						task.uid = m_pIUserItem[i].dwUserID;
						timer.schedule(task,1000 + CommonDef.getRandomInt(4000));
						return;
					}
				}
			}
		}
	}

	// 聊天
	public void Handle_onChat(int _Uid, String[] vecArg) {
		int wChairID = FindSeat(_Uid);
		if (wChairID == Constants.INVALID_CHAIR) {
			System.out.printf("Handle_onChat Error _Uid(%d)(%d)\n", _Uid, wChairID);
			return;
		}
		if (vecArg.length < 2)
			return;
		String strContent = vecArg[1];

		SUBCMD_S_Chat cmd = new SUBCMD_S_Chat();
		cmd.m_lUid = _Uid;
		cmd.m_wUserChairID = wChairID;
		cmd.szContent = strContent;

		SendTableData(Constants.INVALID_CHAIR, cmd.getJsonData());

	}

	// 语音
	public void Handle_onVoice(int _Uid, String[] vecArg) {
		int wChairID = FindSeat(_Uid);
		if (wChairID == Constants.INVALID_CHAIR) {
			System.out.printf("Handle_onVoice Error _Uid(%d)(%d)\n", _Uid, wChairID);
			return;
		}
		if (vecArg.length < 3)
			return;
		int seconds = 0;
		try
		{
			seconds = (int) Float.parseFloat(vecArg[1]);
		}catch(Exception e)
		{
			
		}

		String strVoiceURL = vecArg[2];

		SUBCMD_S_Voice cmd = new SUBCMD_S_Voice();
		cmd.m_lUid = _Uid;
		cmd.m_wUserChairID = wChairID;
		cmd.seconds = seconds;
		cmd.szVoiceURL = strVoiceURL;

		SendTableData(Constants.INVALID_CHAIR, cmd.getJsonData());

	}
	void Handle_onVoiceMsg(int _Uid, String[] vecArg) {
		int wChairID = FindSeat(_Uid);
		if (wChairID == Constants.INVALID_CHAIR) {
			return;
		}
		float seconds = Float.parseFloat(vecArg[1]);
		
		SUBCMD_S_VoiceMsg cmd = new SUBCMD_S_VoiceMsg();
		cmd.m_lUid = _Uid;
		cmd.m_wUserChairID = wChairID;
		cmd.seconds = seconds;
		
		cmd.data = vecArg[2];
		cmd.m_maxClip = Integer.parseInt(vecArg[3]);
		cmd.m_currClip = Integer.parseInt(vecArg[4]);
		SendData(Constants.INVALID_CHAIR,cmd.getJsonData());
	}
	void Handle_onExpr(int _Uid, String[] vecArg) {
		int wChairID = FindSeat(_Uid);
		if (wChairID == Constants.INVALID_CHAIR) {
			return;
		}
		int uid1 = Integer.parseInt(vecArg[1]);
		int uid2 = Integer.parseInt(vecArg[2]);
		int index = Integer.parseInt(vecArg[3]);
		
		SUBCMD_S_ExprMsg cmd = new SUBCMD_S_ExprMsg();
		cmd.m_uid1 = uid1;
		cmd.m_uid2 = uid2;
		cmd.m_index = index;
		
		SendData(Constants.INVALID_CHAIR, cmd.getJsonData());
	}
	// 送礼
	public void Handle_onSongLi(int _Uid, String[] vecArg) {
		SUBCMD_S_SongLi cmd = new SUBCMD_S_SongLi();
		// cmd.m_lUid = _Uid;
		// cmd.m_srcUid;//哪个玩家ID获得
		// cmd.m_dstUid;//玩家座位 号
		// cmd.m_iLiWu;
		// cmd.m_srcCoin;//送礼的人余下多少钱
		// cmd.m_dstCoin;//收礼的人余下多少钱
		SendTableData(Constants.INVALID_CHAIR, cmd.getJsonData());

	}

	// 语音
	public void Handle_onTryDismissRoom(int _Uid, String[] vecArg) {
		int bySeatID = FindSeat(_Uid);
		if (bySeatID == Constants.INVALID_CHAIR) {
			System.out.printf("Handle_onTryDismissRoom Error _Uid(%d)(%d)\n", _Uid, bySeatID);
			return;
		}
		if (vecArg.length < 2)
			return;
		String attachInfo = vecArg[1];
		if(attachInfo.equals("fzquit")){
			//房主请求直接退出
			OnEventGameEnd(0,GAME_END_DISMISS);
			return;
		}
		int cntUser = 0;
		int cntOnline = 0;
		int cntOffline = 0;
		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].dwUserID > 0 && (m_pIUserItem[i].isAndroidUser == 0 || m_pIUserItem[i].isAndroidUser == 2)) {
				cntUser++;
			}
		}
		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].dwUserID > 0 && (m_pIUserItem[i].isAndroidUser == 0 || m_pIUserItem[i].isAndroidUser == 2)) {
				if (m_pIUserItem[i].cbOnline == Constants.US_OFFLINE) {
					cntOffline++;
				} else {
					cntOnline++;
				}
			}
		}
		m_WhoTryDismiss = _Uid;
		if (cntUser > 1) {
			if (m_dismissTimer == 0) {
				m_dismissTimer = System.currentTimeMillis() / 1000;
			}
			m_byAgreeDismiss[bySeatID] = 1;

			SUBCMD_S_TryDismissRoom cmd = new SUBCMD_S_TryDismissRoom();
			cmd.lUid = _Uid;
			cmd.dwTableID = m_dwTableID;
			cmd.szNickName = m_pIUserItem[bySeatID].szNickName;

			SendTableData(Constants.INVALID_CHAIR, cmd.getJsonData());

			if (cntOnline == 1) {
				m_WhoTryDismiss = _Uid;
				OnEventGameEnd(0, GAME_END_DISMISS);

				m_dismissTimer = (long) 0;
			}else{
				//处理robot，同意
				boolean hasRobot = false;
				for(int i = 0;i < 5;i++){
					if( m_pIUserItem[i].isAndroidUser == 2){
						hasRobot = true;
						break;
					}
				}
				if(hasRobot){
					try{
						class AgreeTask extends TimerTask{
		
							@Override
							public void run() {
								for(int i = 0;i < 5;i++){
									if( m_pIUserItem[i].isAndroidUser == 2){
										if(m_isDestoryed == false && m_byAgreeDismiss[i] == 0 && CommonDef.getRandomInt(100) > 50){
											String vecArg[] = {"agree_dismissroom","" + m_pIUserItem[i].dwUserID};
											Handle_onAgreeDismissRoom(m_pIUserItem[i].dwUserID,vecArg);
											return;
										}
									}
								}
							}					
						}
						Timer timer = new Timer();
						AgreeTask task = new AgreeTask();
						timer.schedule(task, 2000,2000);
					}catch(Exception e){
						
					}
				}
			}
		} else {
			m_WhoTryDismiss = _Uid;
			OnEventGameEnd(0, GAME_END_DISMISS);
		}
	}

	// 语音
	public void Handle_onAgreeDismissRoom(int _Uid, String[] vecArg) {
		int bySeatID = FindSeat(_Uid);
		if (bySeatID == Constants.INVALID_CHAIR) {
			System.out.printf("Handle_onAgreeDismissRoom Error _Uid(%d)(%d)\n", _Uid, bySeatID);
			return;
		}
		if (vecArg.length < 2)
			return;
		m_byAgreeDismiss[bySeatID] = 1;

		SUBCMD_S_AgreeDismissRoom cmd = new SUBCMD_S_AgreeDismissRoom();
		cmd.lUid = _Uid;
		cmd.dwTableID = m_dwTableID;
		cmd.szNickName = m_pIUserItem[bySeatID].szNickName;

		SendTableData(Constants.INVALID_CHAIR, cmd.getJsonData());

		if (bySeatID == 0 && m_bTableStarted == false) {
			OnEventGameEnd(0, GAME_END_DISMISS);
			return;
		}
		int cntAgree = 0;
		for (int i = 0; i < 5; i++) {
			if (m_byAgreeDismiss[i] > 0) {
				cntAgree++;
			}
		}
		int cntUser = 0;
		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].dwUserID > 0 &&  (m_pIUserItem[i].isAndroidUser == 0 || m_pIUserItem[i].isAndroidUser == 2)) {
				cntUser++;
			}
		}
		if (cntAgree >= 2 && cntAgree >= cntUser - 1) {
			OnEventGameEnd(0, GAME_END_DISMISS);
		}
	}

	// 语音
	public void Handle_onRefuseDismissRoom(int _Uid, String[] vecArg) {
		int bySeatID = FindSeat(_Uid);
		if (bySeatID == Constants.INVALID_CHAIR) {
			System.out.printf("Handle_onRefuseDismissRoom Error _Uid(%d)(%d)\n", _Uid, bySeatID);
			return;
		}
		m_dismissTimer = (long) 0;
		if (vecArg.length < 2)
			return;

		m_dismissTimer = (long) 0;
		m_byAgreeDismiss[0] = 0;
		m_byAgreeDismiss[1] = 0;
		m_byAgreeDismiss[2] = 0;
		m_byAgreeDismiss[3] = 0;
		m_byAgreeDismiss[4] = 0;

		SUBCMD_S_RefuseDismissRoom cmd = new SUBCMD_S_RefuseDismissRoom();
		cmd.lUid = _Uid;
		cmd.dwTableID = m_dwTableID;
		cmd.szNickName = m_pIUserItem[bySeatID].szNickName;

		SendTableData(Constants.INVALID_CHAIR, cmd.getJsonData());

	}

	public void Handle_onStart(int _Uid, String[] vecArg) {
		if (vecArg.length < 2)
			return;

		int bySeatID = FindSeat(_Uid);
		if (m_bTableStarted == false && bySeatID == 0 && GetPlayerCount() > 1) {
			if (m_bGameStarted == false)
				OnUserReady(bySeatID);
			// OnEventGameStart();
		}
	}

	public void SetParam(String szParam) {
		m_strParam = szParam;
	}

	public void SendUserScore(int _Uid) {
		SHMUser user = dthall.getInstance().getUserInfo(_Uid);
		if (user != null) {
			int wChairID = FindSeat(_Uid);

			SUBCMD_S_UserScore cmd = new SUBCMD_S_UserScore();
			cmd.m_lUid = _Uid;
			cmd.m_lUserScore = m_pIUserItem[wChairID].lUserScore;
			cmd.m_lUserGem = user.getGem(0);
			cmd.m_iVipLevel = user.getVip();

			SendTableData(Constants.INVALID_CHAIR, cmd.getJsonData());

		}
	}

	// 同步分数
	public void Handle_onRenewScore(int _Uid, String[] vecArg) {
		SendUserScore(_Uid);
	}

	// 送礼物
	public void Handle_onUserTransferGift(int _Uid, String[] vecArg) {
		int wChairID = FindSeat(_Uid);
		if (wChairID == Constants.INVALID_CHAIR) {
			System.out.printf("Handle_onUserTransferGift Error ChairID(%d)(%d)\n", m_wCurrentUser, wChairID);
			return;
		}
	}

	public boolean m_bRemove = false;

	public void DelayRemove() {
		m_bRemove = true;
	}

	public void ResetKillFlag() {
		m_bRemove = false;
	}

	public boolean ShouldBeRemoved() {
		return m_bRemove;
	}
	
	public void Handle_onLock(Integer lUid, String[] vecArg) {
		
		if (vecArg.length < 2)
			return;
		if ( m_bTableStarted && m_pIUserItem[0].dwUserID == lUid) {//已经开始 并且是房主
			int index = Constants.atoi(vecArg[1]);
			m_locked = index;			
			
		}
	}
	void Handle_InBack(int _Uid, String vecArg[]) {
		int wChairID = FindSeat(_Uid);
		if (wChairID == Constants.INVALID_CHAIR) {
			return;
		}
		m_pIUserItem[wChairID].cbOnline = Constants.US_OFFLINE;
		SUBCMD_S_InBack cmd = new SUBCMD_S_InBack();
		cmd.byActionUser = wChairID;
		SendData(Constants.INVALID_CHAIR,cmd.getJsonData());
	}

	void Handle_InFore(int _Uid, String vecArg[]) {
		int wChairID = FindSeat(_Uid);
		if (wChairID == Constants.INVALID_CHAIR) {
			return;
		}

		m_pIUserItem[wChairID].cbOnline = 0;
		SUBCMD_S_InFore cmd = new SUBCMD_S_InFore();
		cmd.byActionUser = wChairID;
		SendData(Constants.INVALID_CHAIR,cmd.getJsonData());
	}
	void Handle_GameIap(int _Uid,String vecArg[]){
		int wChairID = FindSeat(_Uid);
		if (wChairID == Constants.INVALID_CHAIR) {
			return;
		}
		SHMUser user = dthall.getInstance().getUserInfo(_Uid);
		if(user == null){
			return;
		}
		SUBCMD_S_GameIap cmd = new SUBCMD_S_GameIap();
		cmd.uid = _Uid;
		cmd.seat = wChairID;
		if(this.isDwcType()){
			cmd.coin = user.getRmb1v1Value();
			m_pIUserItem[wChairID].lUserRmb = cmd.coin;
		}else if(this.isGoldType()){
			cmd.coin = user.getCoin(0);
			m_pIUserItem[wChairID].lUserCoin = (int) cmd.coin;
		}
		SendData(Constants.INVALID_CHAIR,cmd.getJsonData());
	}
	private int m_needAddRobot = 0;
	public void Handle_QuickMatch(int _Uid,String vecArg[]){
		try{
			int wChairID = FindSeat(_Uid);
			if (wChairID == Constants.INVALID_CHAIR) {
				return;
			}
			SHMUser user = dthall.getInstance().getUserInfo(_Uid);
			if(user == null){
				return;
			}
			//add robot
			class AddRobotTask extends TimerTask{
				public Timer _timer;
				@Override
				public void run() {
					if(CommonDef.getRandomInt(100) > 75)
						return;
					int rev = addRobotPlayer();
					if(rev < 0){
						_timer.cancel();
					}
				}
			}
			if(m_needAddRobot == 0)
				m_needAddRobot = 2 + CommonDef.getRandomInt(3);
			Timer timer = new Timer();
			AddRobotTask task = new AddRobotTask();
			task._timer = timer;
			timer.schedule(task,2000,3000);
		}catch(Exception e){
			
		}
	}
	public int addRobotPlayer(){
		int userCnt = 0;
		int robotCnt = 0;
		for(int i = 0;i < 5;i++){
			if(m_pIUserItem[i].dwUserID > 0){
				userCnt++;
			}
			if(m_pIUserItem[i].dwUserID > 0 && m_pIUserItem[i].isAndroidUser == 2){
				robotCnt++;
			}
		}
		if(userCnt == 5){
			SUBCMD_S_QuickMatchFinish cmd = new SUBCMD_S_QuickMatchFinish();
			SendData(Constants.INVALID_CHAIR, cmd.getJsonData());
			return -1;
		}
		if(robotCnt >= m_needAddRobot){
			SUBCMD_S_QuickMatchFinish cmd = new SUBCMD_S_QuickMatchFinish();
			SendData(Constants.INVALID_CHAIR, cmd.getJsonData());
			return -1;
		}
		Robot robot = RobotMgr.getInstance().AllocRobot();
		int robot_uid = robot.uid;
		
		int robot_gold = (int) RobotMgr.getInstance().getRandRobotGold(m_ruchang,m_difen);
		SHMUser user = dthall.getInstance().getUserInfo(robot_uid);
		if(user == null)
			return 0;
		user.m_lTotalCoin = robot_gold;		

		ArrayList<Integer> vecEmpty = new ArrayList<>();
		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].dwUserID == 0) {
				vecEmpty.add(i);
			}
		}
		if (vecEmpty.size() > 0) {
			if(this.m_clubId != 0){
				m_wRobotYongQi[vecEmpty.get(0)] = Constants.rand(2000,8000);
				GameAgent agent = new GameAgent(user);
				net.sf.json.JSONObject json = new net.sf.json.JSONObject();
				String data = "JoinRoom|" + this.m_clubId + "|" + this.m_dwTableID;
				json.put("data", data);
				ClubMgr.getInstance().m_clubHandler.HandleOn_ClubInfo(agent, json);
			}else{
				OnUserSit(robot_uid);
			}
			// 主动准备
			Handle_onReady(robot_uid, new String[2]);
			return 1;
		}
		return 1;
	}
	public void DoAddGoldRobotPlayer(){
		int userCnt = 0;
		int robotCnt = 0;
		for(int i = 0;i < 5;i++){
			if(m_pIUserItem[i].dwUserID > 0){
				userCnt++;
			}
			if(m_pIUserItem[i].dwUserID > 0 && m_pIUserItem[i].isAndroidUser == 2){
				robotCnt++;
			}
		}
		if(userCnt == 5){
			setNeedRobot(false,0);
			return;
		}
		int robot_gold = (int) RobotMgr.getInstance().getRandRobotGold(m_ruchang,m_difen);
		int gold = GoldMgr.getInstance().applyRobotGold(robot_gold);
		if(gold == 0){
			//申请不到足够的金币
			setNeedRobot(false,0);
			return;
		}
		Robot robot = RobotMgr.getInstance().AllocRobot();
		int robot_uid = robot.uid;
		
		
		SHMUser user = dthall.getInstance().getUserInfo(robot_uid);
		if(user == null){
			setNeedRobot(false,0);
			return;
		}
		user.m_lTotalCoin = gold;
		user.setApplyNum(gold);

		ArrayList<Integer> vecEmpty = new ArrayList<>();
		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].dwUserID == 0) {
				vecEmpty.add(i);
			}
		}
		if (vecEmpty.size() > 0) {
			if(this.m_clubId != 0){
				m_wRobotYongQi[vecEmpty.get(0)] = Constants.rand(2000,8000);
				GameAgent agent = new GameAgent(user);
				net.sf.json.JSONObject json = new net.sf.json.JSONObject();
				String data = "JoinRoom|" + this.m_clubId + "|" + this.m_dwTableID;
				json.put("data", data);
				ClubMgr.getInstance().m_clubHandler.HandleOn_ClubInfo(agent, json);
			}else{
				OnUserSit(robot_uid);
			}
			// 主动准备
			class GoldReadyTask extends TimerTask{
				@Override
				public void run() {
					Handle_onReady(robot_uid, new String[2]);					
				}				
			}
			Timer timer = new Timer();
			timer.schedule(new GoldReadyTask(),1000 * CommonDef.randInt(1, 4));
		}
	}
	public void DoAddRmbRobotPlayer(){
		int userCnt = 0;
		int robotCnt = 0;
		for(int i = 0;i < 5;i++){
			if(m_pIUserItem[i].dwUserID > 0){
				userCnt++;
			}
			if(m_pIUserItem[i].dwUserID > 0 && m_pIUserItem[i].isAndroidUser == 2){
				robotCnt++;
			}
		}
		if(userCnt == 5){
			setNeedRobot(false,0);
			return;
		}
		double robot_rmb = RobotMgr.getInstance().getRandRobotGold(m_ruchang,m_doubleDiFen);
		robot_rmb += Math.random() * 2;
		BigDecimal b = new BigDecimal(robot_rmb);
		robot_rmb = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(); 
		double rmb = GoldMgr.getInstance().applyRobotRmb(robot_rmb);
		if(rmb <= 0.0){
			//申请不到足够的rmb
			setNeedRobot(false,0);
			return;
		}
		Robot robot = RobotMgr.getInstance().AllocRobot();
		int robot_uid = robot.uid;
		
		
		SHMUser user = dthall.getInstance().getUserInfo(robot_uid);
		if(user == null){
			setNeedRobot(false,0);
			return;
		}
		user.setRMb1v1Value(rmb);
		user.setApplyNum(rmb);

		ArrayList<Integer> vecEmpty = new ArrayList<>();
		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].dwUserID == 0) {
				vecEmpty.add(i);
			}
		}
		if (vecEmpty.size() > 0) {
			if(this.m_clubId != 0){
				m_wRobotYongQi[vecEmpty.get(0)] = Constants.rand(2000,8000);
				GameAgent agent = new GameAgent(user);
				net.sf.json.JSONObject json = new net.sf.json.JSONObject();
				String data = "JoinRoom|" + this.m_clubId + "|" + this.m_dwTableID;
				json.put("data", data);
				ClubMgr.getInstance().m_clubHandler.HandleOn_ClubInfo(agent, json);
			}else{
				OnUserSit(robot_uid);
			}
			// 主动准备
			class DwcReadyTask extends TimerTask{
				@Override
				public void run() {
					Handle_onReady(robot_uid, new String[2]);					
				}				
			}
			Timer timer = new Timer();
			timer.schedule(new DwcReadyTask(),1000 * CommonDef.randInt(1, 4));
		}
	}
	public void DoAddRobotPlayer(){
		if(m_roomType.equals("GOLD")){
			DoAddGoldRobotPlayer();
		}else if(m_roomType.equals("DWC")){
			DoAddRmbRobotPlayer();
		}
	}
	@Override
	public String getRoomInfo() {
		// TODO 自动生成的方法存根
		//m_lStartTime
		//m_pIUserItem
		// m_bTableStarted;
		// m_byRound;
		// m_byMaxRound;
		//m_dwTableID
		String roomInfo = String.format("%s/%s/%s/%s/%s", 
				m_pIUserItem[0].GetUserInfo(), //#
				m_pIUserItem[1].GetUserInfo(), 
				m_pIUserItem[2].GetUserInfo(), 
				m_pIUserItem[3].GetUserInfo(),
				m_pIUserItem[4].GetUserInfo()
				);
		String[] vecParam = m_strParam.split(";");
		String vecParamStr = "";
		for (int i = 0; i < vecParam.length; i++) {
			String param = vecParam[i];

			if (param.equalsIgnoreCase("AAPAY")) {
				vecParamStr += "AA支付 ";
			}
			if (param.equalsIgnoreCase("FZPAY")) {
				vecParamStr += "房主支付 ";
			}
			if (param.equalsIgnoreCase("CLUBPAY")) {
				vecParamStr += "会长支付 ";
			}
			if (param.equalsIgnoreCase("8JU")) {
				vecParamStr += "8局 ";
			}
			if (param.equalsIgnoreCase("16JU")) {
				vecParamStr += "16局 ";
			}
			if (param.equalsIgnoreCase("24JU")) {
				vecParamStr += "24局 ";
			}
			
			if (param.equalsIgnoreCase("FD30"))
			{
				vecParamStr += "限注30 ";
			}
			if (param.equalsIgnoreCase("FD50"))
			{
				vecParamStr += "限注50 ";
			}
			if (param.equalsIgnoreCase("FD80"))
			{
				vecParamStr += "限注80 ";
			}		
			if (param.equalsIgnoreCase("FD100"))
			{
				vecParamStr += "低分100 ";
			}
			if (param.equalsIgnoreCase( "DI1"))
			{
				vecParamStr += "底分1分 ";
			}
			if (param.equalsIgnoreCase("DI3"))
			{
				vecParamStr += "底分3分 ";						
			}
			if (param.equalsIgnoreCase("DI5"))
			{
				vecParamStr += "底分5分 ";

			}
			if (param.equalsIgnoreCase( "MEN1"))
			{
				vecParamStr += "闷一圈";
			}
			if (param.equalsIgnoreCase("MEN3"))
			{
				vecParamStr += "闷三圈 ";						
			}	
		}
		
		int start = 0;
		if(m_bTableStarted){
			start = 1;
		}
		String descInfo = String.format("炸金花 %s",vecParamStr);
		roomInfo = String.format("%s|%d|%d|%d|%s|%d|%d|%d|%s|%S", roomInfo,m_dwTableID,m_lStartTime,start,descInfo,maxPeople,m_byRound,m_byMaxRound,"炸金花","ZJH");
		
		return roomInfo;
	}
	@Override
	public int getPlayerCount() {
		// TODO Auto-generated method stub
		return GetPlayerCount();
	}

	@Override
	public int getStatus() {
		// TODO Auto-generated method stub
		if(m_bTableStarted == false)
		{
			return 0;
		}
		return 1;
	}
	@Override
	public String getRoomDesc() {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public int getCellScore() {
		// TODO Auto-generated method stub
		return 0;
	}
	@Override
	public int getMinScore() {
		// TODO Auto-generated method stub
		return 0;
	}
	@Override
	public String getPayway() {
		// TODO Auto-generated method stub
		return m_payWay;
	}
	@Override
	public int getCostGem() {
		// TODO Auto-generated method stub
		return m_byCostGem;
	}
	@Override
	public String GetRoundInfo() {
		return "" + m_byRound + "/" +  m_byMaxRound;
	}
}